Thirteen problems that drill tree DFS from Lesson 13. The skill is recursion: for each node, decide what the call returns and how it combines the left and right results. Read the approach, try it yourself, then check the code — every solution here was run and tested.
How to practise. For each problem, write the recurrence in one sentence first (“return the depth of the deeper child plus one”), then code it. If a global answer is needed (diameter, max path sum), separate “what I return to my parent” from “the best answer seen so far.” Stuck for 20+ minutes? Read it, close the page, re-solve from memory the next day.
All solutions assume this node definition:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val; self.left = left; self.right = rightThe 13 problems
- 1Maximum Depth of Binary Tree (LeetCode 104)EasyFind how many levels deep a binary tree goes: a node's depth is one plus its deeper child's depth.arrow_forward
- 2Same Tree (LeetCode 100)EasyCheck whether two binary trees are identical: walk both together and compare node by node.arrow_forward
- 3Invert Binary Tree (LeetCode 226)EasyMirror a binary tree left-to-right: swap every node's two children, recursively.arrow_forward
- 4Path Sum (LeetCode 112)EasyIs there a root-to-leaf path adding up to a target? Subtract as you descend; check zero at a leaf.arrow_forward
- 5Subtree of Another Tree (LeetCode 572)EasyDoes one tree appear inside another? At every node of the big tree, run a Same Tree check.arrow_forward
- 6Diameter of Binary Tree (LeetCode 543)EasyLongest path between any two nodes: return depth upward, but record left + right depth at each node.arrow_forward
- 7Lowest Common Ancestor of a BST (LeetCode 235)MediumFind the deepest shared ancestor of two BST nodes: walk down until the two values split sides.arrow_forward
- 8Lowest Common Ancestor of a Binary Tree (LeetCode 236)MediumDeepest shared ancestor of two nodes in any binary tree: the node where one is found on each side.arrow_forward
- 9Validate Binary Search Tree (LeetCode 98)MediumCheck a tree is a valid BST: pass a (low, high) range down and make every node fit strictly inside it.arrow_forward
- 10Kth Smallest Element in a BST (LeetCode 230)MediumFind the kth smallest value in a BST: in-order traversal visits values in sorted order, so stop at pop k.arrow_forward
- 11Construct Binary Tree from Preorder and Inorder (LeetCode 105)MediumRebuild a tree from its preorder and inorder lists: preorder names each root, inorder splits its subtrees.arrow_forward
- 12Serialize and Deserialize Binary Tree (LeetCode 297)HardTurn a tree into a string and back: write preorder with '#' for nulls, then read it back in the same order.arrow_forward
- 13Binary Tree Maximum Path Sum (LeetCode 124)HardBest-sum path anywhere in a tree: return the best one-sided gain upward, track node + left + right globally.arrow_forward