LeetCode ยท Trees
Trees Problem Set
Binary trees, BSTs, and tree traversals. Most tree problems are recursive โ think "what does each node need from its children?"
Concept pages:
Binary Tree ยท
BST
| Difficulty | Pattern | Problem | Key Insight |
|---|---|---|---|
| Easy | recursion | LC 226 ยท Invert Binary Tree | Swap left and right children recursively. O(n). |
| Easy | recursion | LC 104 ยท Maximum Depth of Binary Tree | 1 + max(left depth, right depth). O(n). |
| Easy | recursion | LC 101 ยท Symmetric Tree | Mirror recursion compares left.left with right.right and left.right with right.left. |
| Easy | recursion | LC 100 ยท Same Tree | Compare structure and values recursively. O(n). |
| Easy | recursion | LC 572 ยท Subtree of Another Tree | Check isSameTree at every node. O(mยทn). |
| Easy | traversal | LC 94 ยท Binary Tree Inorder Traversal | Left โ root โ right. Iterative with stack or recursive. O(n). |
| Medium | BST | LC 98 ยท Validate Binary Search Tree | Pass min/max bounds down. O(n). |
| Medium | BFS | LC 102 ยท Binary Tree Level Order Traversal | BFS with queue, group by level. O(n). |
| Medium | construction | LC 105 ยท Construct Binary Tree from Preorder and Inorder | Preorder root + inorder split. HashMap for O(1) index lookup. O(n). |
| Medium | LCA | LC 236 ยท Lowest Common Ancestor of a Binary Tree | If one target is in each subtree, the current node is the first split point and therefore the LCA. |
| Medium | BST | LC 230 ยท Kth Smallest Element in a BST | Inorder traversal, count to k. O(H + k). |
| Medium | LCA | LC 235 ยท Lowest Common Ancestor of a BST | If both < root go left, both > root go right, else root is LCA. O(H). |
| Hard | path | LC 124 ยท Binary Tree Maximum Path Sum | At each node: max single-path vs split-path. Track global max. O(n). |