Trees — Complete Pattern Guide
Problems 346–420 · 75 posts · Easy × 15, Medium × 40, Hard × 20
Core Patterns
| # | Pattern | Key Idea | Example |
|---|
| 1 | Inorder / Preorder / Postorder | Recursive DFS | Tree traversals, BST validation |
| 2 | Level Order BFS | Queue layer by layer | Level order, zigzag, right view |
| 3 | Path Sum | DFS with running sum | Path Sum I/II/III, Max Path Sum |
| 4 | BST Property | Left < root < right | Validate BST, Search BST, Insert/Delete |
| 5 | LCA (Lowest Common Ancestor) | Find split point | LCA of BST/BT, Distance |
| 6 | Tree Construction | From traversals | Build from Preorder+Inorder |
| 7 | Serialize/Deserialize | BFS or DFS encoding | Serialize BT, Codec |
| 8 | Tree DP | dp on subtrees | Diameter, Max Path, House Robber III |
| 9 | Morris Traversal | O(1) space inorder | Inorder without recursion/stack |
Templates
DFS Traversals
def inorder(root):
if not root: return []
return inorder(root.left) + [root.val] + inorder(root.right)
def preorder(root):
if not root: return []
return [root.val] + preorder(root.left) + preorder(root.right)
def postorder(root):
if not root: return []
return postorder(root.left) + postorder(root.right) + [root.val]
Iterative Inorder
def inorder_iter(root):
stack, result = [], []
curr = root
while curr or stack:
while curr: stack.append(curr); curr = curr.left
curr = stack.pop()
result.append(curr.val)
curr = curr.right
return result
BFS Level Order
from collections import deque
def level_order(root):
if not root: return []
queue, result = deque([root]), []
while queue:
level = []
for _ in range(len(queue)):
node = queue.popleft()
level.append(node.val)
if node.left: queue.append(node.left)
if node.right: queue.append(node.right)
result.append(level)
return result
Path Sum DFS
def has_path_sum(root, target):
if not root: return False
if not root.left and not root.right: return root.val == target
return has_path_sum(root.left, target-root.val) or has_path_sum(root.right, target-root.val)
LCA Template
def lca(root, p, q):
if not root or root == p or root == q: return root
left = lca(root.left, p, q)
right = lca(root.right, p, q)
return root if left and right else left or right
Tree DP (diameter pattern)
def diameter(root):
self.ans = 0
def depth(node):
if not node: return 0
L, R = depth(node.left), depth(node.right)
self.ans = max(self.ans, L+R)
return max(L, R)+1
depth(root)
return self.ans
Problem Index — Trees
Easy (346–360)
| # | Problem |
|---|
| 346 | Maximum Depth of Binary Tree |
| 347 | Minimum Depth of Binary Tree |
| 348 | Invert Binary Tree |
| 349 | Symmetric Tree |
| 350 | Path Sum I |
| 351 | Same Tree |
| 352 | Count Complete Tree Nodes |
| 353 | Balanced Binary Tree |
| 354 | Merge Two Binary Trees |
| 355 | Sum of Left Leaves |
| 356 | Find Mode in BST |
| 357 | Range Sum of BST |
| 358 | Search in BST |
| 359 | Insert into BST |
| 360 | Convert Sorted Array to BST |
Medium (361–395)
| # | Problem |
|---|
| 361 | Binary Tree Level Order Traversal |
| 362 | Binary Tree Zigzag Level Order |
| 363 | Binary Tree Right Side View |
| 364 | Average of Levels |
| 365 | Path Sum II |
| 366 | Path Sum III |
| 367 | Diameter of Binary Tree |
| 368 | Flatten Binary Tree to LL |
| 369 | Construct from Preorder + Inorder |
| 370 | Construct from Inorder + Postorder |
| 371 | Validate BST |
| 372 | Kth Smallest in BST |
| 373 | Delete Node in BST |
| 374 | LCA of Binary Tree |
| 375 | LCA of BST |
| 376 | Binary Tree Level Order II (bottom-up) |
| 377 | Populating Next Right Pointers |
| 378 | Binary Tree Maximum Path Sum (prep) |
| 379 | All Nodes Distance K in Binary Tree |
| 380 | Sum Root to Leaf Numbers |
| 381 | Maximum Width of Binary Tree |
| 382 | Find Leaves of Binary Tree |
| 383 | Step-By-Step Directions |
| 384 | Check Completeness of Binary Tree |
| 385 | Count Good Nodes |
| 386 | Convert BST to Greater Tree |
| 387 | Recover BST (swap 2 nodes) |
| 388 | House Robber III |
| 389 | Find Duplicate Subtrees |
| 390 | Serialize and Deserialize BST |
| 391 | Unique BSTs (Catalan Number) |
| 392 | Unique BSTs II |
| 393 | Trim a BST |
| 394 | Lowest Common Ancestor (all variations) |
| 395 | Vertical Order Traversal |
Hard (396–420)
| # | Problem |
|---|
| 396 | Binary Tree Maximum Path Sum |
| 397 | Serialize and Deserialize Binary Tree |
| 398 | Recover Binary Search Tree |
| 399 | Binary Tree Cameras |
| 400 | Count of Smaller Numbers (BST) |
| 401 | Vertical Order Traversal (hard) |
| 402 | Distribute Coins in Binary Tree |
| 403 | Maximum Sum BST in Binary Tree |
| 404 | Number of Ways to Reorder Array to Same BST |
| 405 | Closest Leaf in a Binary Tree |
| 406 | Flip Binary Tree to Match Preorder |
| 407 | Build BST from Preorder Traversal |
| 408 | Largest BST Subtree |
| 409 | Kth Ancestor of a Tree Node |
| 410 | Step-By-Step Directions Hard |
| 411 | Maximum Depth of N-ary Tree |
| 412 | Serialize and Deserialize N-ary Tree |
| 413 | LCA with Parent Pointers |
| 414 | Find Root of N-ary Tree |
| 415 | Trees Master Recap |