Data Structures & Algorithms · Revision notes
Binary Trees & BST
A binary tree has at most two children per node. DFS (pre/in/post-order) and BFS (level order) traverse it in O(n); a BST keeps left < node < right so search, insert and delete cost O(h), which is O(log n) when balanced and O(n) when skewed.
Quick revision
- n0 = n2 + 1; max nodes 2^(h+1)-1; min height floor(log2 n)
- Pre: root L R; In: L root R; Post: L R root; BFS queue
- In + pre/post reconstructs uniquely; pre + post does not
- BST ops O(h); delete 2 children -> inorder successor; validate with bounds
- LCA: BST by comparison O(h); general tree recursion O(n); dist = d(p)+d(q)-2d(lca)