What is the difference between a binary tree and a binary search tree?
Simple meaning
A binary tree gives each node at most two children with no ordering.
Open the full page for Why, Steps, Example and Key takeaway.
Panel-ready DSA interview set questions for freshers and experienced developers. Practice at Coding Cadre in Faridabad, or Online from Delhi NCR.
A binary tree gives each node at most two children with no ordering.
Open the full page for Why, Steps, Example and Key takeaway.
Inorder is left-root-right, preorder is root-left-right, and postorder is left-right-root.
Open the full page for Why, Steps, Example and Key takeaway.
Height is 1 plus the max of left and right subtree heights, with empty as -1 or 0 depending on the convention you state.
Open the full page for Why, Steps, Example and Key takeaway.
BFS with a queue: start from the root and enqueue children as you dequeue parents.
Open the full page for Why, Steps, Example and Key takeaway.
From the root, go left if the target is smaller and right if it is larger, until you hit the node or null.
Open the full page for Why, Steps, Example and Key takeaway.
Walk the same search path until a null child, then attach a new node there, rejecting duplicates if the tree forbids them.
Open the full page for Why, Steps, Example and Key takeaway.
The BST invariant means every left key is smaller and every right key is larger, so left-root-right emits keys in order.
Open the full page for Why, Steps, Example and Key takeaway.
A binary heap is a complete binary tree stored in an array with parent-child index math.
Open the full page for Why, Steps, Example and Key takeaway.
Keep a min-heap of size k: push each number and pop when the heap grows past k so the root is the kth largest.
Open the full page for Why, Steps, Example and Key takeaway.
BFS explores level by level and finds shortest paths in unweighted graphs, using a queue.
Open the full page for Why, Steps, Example and Key takeaway.
An adjacency list stores neighbors per vertex: O(V+E) space and fast iteration over edges, best for sparse graphs.
Open the full page for Why, Steps, Example and Key takeaway.
Iterate vertices
Open the full page for Why, Steps, Example and Key takeaway.
Naive recursion is exponential because it recomputes subproblems.
Open the full page for Why, Steps, Example and Key takeaway.
The number of ways to reach n is ways(n-1) plus ways(n-2), the same recurrence as Fibonacci.
Open the full page for Why, Steps, Example and Key takeaway.
Taking the largest coin that does not exceed the remainder is optimal for canonical systems like US coins.
Open the full page for Why, Steps, Example and Key takeaway.
Sort activities by finish time and repeatedly pick the next one that starts after the last chosen finish.
Open the full page for Why, Steps, Example and Key takeaway.
Sort greed sizes and cookie sizes, then walk both with two pointers, assigning the smallest cookie that satisfies the current child.
Open the full page for Why, Steps, Example and Key takeaway.
Merge sort always splits and merges in O(n log n) time and uses O(n) extra space for the merge buffers.
Open the full page for Why, Steps, Example and Key takeaway.
Pick a pivot, partition smaller keys left and larger right, then recurse.
Open the full page for Why, Steps, Example and Key takeaway.
Stable sorts keep equal keys in their original relative order.
Open the full page for Why, Steps, Example and Key takeaway.