Tree / Recursion
Tree problems are solved by recursion that processes each node and combines the results from its children. Decide whether you need pre-, in-, or post-order traversal, and what each call should return up to its parent.
37 LeetCode problems solved with the Tree / Recursion pattern. Practice them with spaced repetition so the pattern sticks.
- Balanced Binary TreeEASY · O(n)
- Binary Tree CamerasHARD · O(n)
- Binary Tree Inorder TraversalEASY · O(n)
- Binary Tree Maximum Path SumHARD · O(n)
- Binary Tree PathsEASY · O(n)
- Binary Tree Postorder TraversalEASY · O(n)
- Binary Tree Preorder TraversalEASY · O(n)
- Construct Binary Tree from Inorder and Postorder TraversalMEDIUM · O(n)
- Construct Binary Tree from Preorder and Inorder TraversalMEDIUM · O(n)
- Convert BST to Greater TreeMEDIUM · O(n)
- Count Complete Tree NodesEASY · O(log^2 n)
- Count Good Nodes in Binary TreeMEDIUM · O(n)
- Delete Leaves With a Given ValueMEDIUM · O(n)
- Delete Node in a BSTMEDIUM · O(h)
- Diameter of Binary TreeEASY · O(n)
- Find Mode in Binary Search TreeEASY · O(n)
- Flatten Binary Tree to Linked ListMEDIUM · O(n)
- House Robber IIIMEDIUM · O(n)
- Insert into a Binary Search TreeMEDIUM · O(h)
- Invert Binary TreeEASY · O(n)
- Kth Smallest Element in a BSTMEDIUM · O(H + k)
- Leaf-Similar TreesEASY · O(n + m)
- Longest ZigZag Path in a Binary TreeMEDIUM · O(n)
- Lowest Common Ancestor of a Binary TreeMEDIUM · O(n)
- Maximum Depth of Binary TreeEASY · O(n)
- Merge Two Binary TreesEASY · O(n)
- Minimum Absolute Difference in BSTEASY · O(n)
- Path Sum IIIMEDIUM · O(n)
- Populating Next Right Pointers in Each Node IIMEDIUM · O(n)
- Same TreeEASY · O(n)
- Serialize and Deserialize Binary TreeHARD · O(n)
- Subtree of Another TreeEASY · O(m * n)
- Sum of Left LeavesEASY · O(n)
- Sum Root to Leaf NumbersMEDIUM · O(n)
- Symmetric TreeEASY · O(n)
- Trim a Binary Search TreeMEDIUM · O(n)
- Validate Binary Search TreeMEDIUM · O(n)
See the full solution
The complete approach, reference solutions in 5 languages, and a step-by-step visualization — then add this problem to your spaced-repetition schedule.
Start free →