Practice SetLesson 131 min read

Tree DFS — Practice Problems

November 05, 2026

// How to use this set

Work these in order: easy (Maximum Depth, Same Tree, Invert, Path Sum, Subtree, Diameter) → medium (LCA of a BST, LCA of a Binary Tree, Validate BST, Kth Smallest in a BST, Build Tree from Pre+In) → hard (Serialize/Deserialize, Binary Tree Max Path Sum). Recursion is the whole game: decide what each call returns and what it combines from its children. Every solution below was run and tested.

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 = right

The 13 problems

  1. 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
  2. 2Same Tree (LeetCode 100)EasyCheck whether two binary trees are identical: walk both together and compare node by node.arrow_forward
  3. 3Invert Binary Tree (LeetCode 226)EasyMirror a binary tree left-to-right: swap every node's two children, recursively.arrow_forward
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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