π³ Trees
Trees are everywhere: file systems, ASTs, DBs, the DOM. ~90% of tree problems are "do DFS or BFS and return something". Once you fluently translate problems into traversal + accumulator, this category collapses.
This category contains 77 problems. Use the patterns below to recognize what's being asked, then jump to the problem list at the bottom.
π§ Key Patternsβ
- DFS (recursive) β Most natural for trees. Pre/In/Post-order. Return a value per subtree.
- BFS (queue) β Level-order, shortest path in tree, populating-next pointers.
- BST Property β Left < root < right. In-order traversal yields sorted output.
- LCA β Lowest Common Ancestor: recursion returning subtree containment.
- Serialize / Deserialize β Pre-order with null markers; or level-order BFS.
- Path Problems β Path sum, diameter, max gain β return "best ending here" + update global.
β οΈ Common Pitfallsβ
- Stack overflow on skewed trees β consider iterative when depth > 10β΄.
- Forgetting to handle
nullchildren in recursion. - Mutating a global accumulator without resetting between test cases.
π Study Resourcesβ
πΊ Videosβ
π Booksβ
- Introduction to Algorithms (CLRS) β Ch. 12 (BST), Ch. 13 (Red-Black)
- Algorithm Design Manual β Skiena β Ch. 3.4
π Articles & Referencesβ
π» All Trees Problemsβ
All Nodes Distance K in Binary Tree
LeetCode 893 | Difficulty: Medium
Average of Levels in Binary Tree
LeetCode 637 | Difficulty: Easy
Balanced Binary Tree
LeetCode 110 | Difficulty: Easy
Binary Search Tree Iterator
LeetCode 173 | Difficulty: Medium
Binary Tree Cameras
LeetCode 1008 | Difficulty: Hard
Binary Tree Inorder Traversal
LeetCode 94 | Difficulty: Easy
Binary Tree Level Order Traversal
LeetCode 102 | Difficulty: Medium
Binary Tree Level Order Traversal II
LeetCode 107 | Difficulty: Medium
Binary Tree Longest Consecutive Sequence
LeetCode Link
Binary Tree Paths
LeetCode 257 | Difficulty: Easy
Binary Tree Postorder Traversal
LeetCode 145 | Difficulty: Easy
Binary Tree Preorder Traversal
LeetCode 144 | Difficulty: Easy
Binary Tree Right Side View
LeetCode 199 | Difficulty: Medium
Binary Tree Tilt
LeetCode 563 | Difficulty: Easy
Binary Tree Vertical Order Traversal
LeetCode Link
Binary Tree Zigzag Level Order Traversal
LeetCode 103 | Difficulty: Medium
Boundary of Binary Tree
LeetCode Link
Check Completeness of a Binary Tree
LeetCode 998 | Difficulty: Medium
Closest Binary Search Tree Value
LeetCode Link
Construct Binary Tree from Inorder and Postorder Traversal
LeetCode 106 | Difficulty: Medium
Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 105 | Difficulty: Medium
Convert Binary Search Tree to Sorted Doubly Linked List
LeetCode Link
Convert Sorted Array to Binary Search Tree
LeetCode 108 | Difficulty: Easy
Convert Sorted List to Binary Search Tree
LeetCode 109 | Difficulty: Medium
Correct a Binary Tree
LeetCode Link
Count Complete Tree Nodes
LeetCode 222 | Difficulty: Easy
Count Good Nodes in Binary Tree
LeetCode 1544 | Difficulty: Medium
Count Univalue Subtrees
LeetCode Link
Cousins in Binary Tree
LeetCode 1035 | Difficulty: Easy
Deepest Leaves Sum
LeetCode 1254 | Difficulty: Medium
Delete Node in a BST
LeetCode 450 | Difficulty: Medium
Diameter of Binary Tree
LeetCode 543 | Difficulty: Easy
Distribute Coins in Binary Tree
LeetCode 1021 | Difficulty: Medium
Find All The Lonely Nodes
LeetCode Link
Find Bottom Left Tree Value
LeetCode 513 | Difficulty: Medium
Find Largest Value in Each Tree Row
LeetCode 515 | Difficulty: Medium
Find Leaves of Binary Tree
LeetCode Link
Flatten Binary Tree to Linked List
LeetCode 114 | Difficulty: Medium
Inorder Successor in BST
LeetCode Link
Insert into a Binary Search Tree
LeetCode 784 | Difficulty: Medium
Invert Binary Tree
LeetCode 226 | Difficulty: Easy
Kth Smallest Element in a BST
LeetCode 230 | Difficulty: Medium
Lowest Common Ancestor of a Binary Search Tree
LeetCode 235 | Difficulty: Medium
Lowest Common Ancestor of a Binary Tree
LeetCode 236 | Difficulty: Medium
Maximum Depth of Binary Tree
LeetCode 104 | Difficulty: Easy
Maximum Depth of N-ary Tree
LeetCode 774 | Difficulty: Easy
Merge Two Binary Trees
LeetCode 617 | Difficulty: Easy
Minimum Absolute Difference in BST
LeetCode 530 | Difficulty: Easy
Minimum Depth of Binary Tree
LeetCode 111 | Difficulty: Easy
Minimum Distance Between BST Nodes
LeetCode 799 | Difficulty: Easy
Modification
Populate Next Right Pointers in Each Node
N-ary Tree Level Order Traversal
LeetCode 764 | Difficulty: Medium
N-ary Tree Postorder Traversal
LeetCode 776 | Difficulty: Easy
N-ary Tree Preorder Traversal
LeetCode 775 | Difficulty: Easy
Path Sum
LeetCode 112 | Difficulty: Easy
Path Sum II
LeetCode 113 | Difficulty: Medium
Populating Next Right Pointers in Each Node
LeetCode 116 | Difficulty: Medium
Populating Next Right Pointers in Each Node II
LeetCode 117 | Difficulty: Medium
Range Sum of BST
LeetCode 975 | Difficulty: Easy
Same Tree
LeetCode 100 | Difficulty: Easy
Search in a Binary Search Tree
LeetCode 783 | Difficulty: Easy
Second Minimum Node In a Binary Tree
LeetCode 671 | Difficulty: Easy
Serialize and Deserialize Binary Tree
LeetCode 297 | Difficulty: Hard
Serialize and Deserialize BST
LeetCode 449 | Difficulty: Medium
Serialize and Deserialize N-ary Tree
LeetCode Link
Split BST
LeetCode Link
Subtree of Another Tree
LeetCode 572 | Difficulty: Easy
Sum of Distances in Tree
LeetCode 863 | Difficulty: Hard
Sum Root to Leaf Numbers
LeetCode 129 | Difficulty: Medium
Symmetric Tree
LeetCode 101 | Difficulty: Easy
Two Sum BSTs
LeetCode Link
Unique Binary Search Trees
LeetCode 96 | Difficulty: Medium
Unique Binary Search Trees II
LeetCode 95 | Difficulty: Medium
Univalued Binary Tree
LeetCode 1005 | Difficulty: Easy
Validate Binary Search Tree
LeetCode 98 | Difficulty: Medium
Verify Preorder Sequence in Binary Search Tree
LeetCode Link
Vertical Order Traversal of a Binary Tree
LeetCode 1029 | Difficulty: Hard