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). |