CoursesTrees & Graphs

Tree DFS

Lessons
9
Exercises
57
Minutes
61
  1. 1

    Go Deep First

    Recursion mirrors the tree: dive to a leaf, let null stop you, and combine what the children return.

    Complete codeMultiple choiceTap
    6 exercises
    5 min
  2. 2

    Invert Binary Tree

    One swap per node — recursion carries it to every corner of the tree.

    Complete codeMultiple choiceTrace
    6 exercises
    6 min
  3. 3

    Path Sum

    Subtract as you descend — and remember that only a leaf can finish the path.

    Complete codeDebugMultiple choiceTrace
    7 exercises
    7 min
  4. 4

    Diameter of Binary Tree

    One recursion, two quantities: each node's best path is leftHeight + rightHeight, but the function must hand its parent a height.

    Complete codeDebugMultiple choiceTrace
    6 exercises
    7 min
  5. 5

    LCA of a BST

    BST ordering turns LCA into a single walk: both smaller go left, both larger go right, and the split point is the answer.

    Complete codeMultiple choiceTrace
    6 exercises
    6 min
  6. 6

    Kth Smallest in a BST

    Inorder traversal of a BST visits values in sorted order — count the visits and stop the recursion at the kth.

    Complete codeDebugMultiple choiceTrace
    6 exercises
    7 min
  7. 7

    Tree DFS CheckpointCheckpoint

    No new concepts — transfer the island's DFS moves to unseen trees: walking two trees at once, carrying path state down, and summing subtrees bottom-up.

    Complete codeCode orderMultiple choiceTrace
    6 exercises
    7 min
  8. 8

    Tree DFS ReviewReview

    Mixed recap of the island: state flowing down vs answers flowing up, BST ordering shortcuts, picking a traversal order, base cases, and the price of recomputing heights.

    Complete codeMultiple choiceTrace
    6 exercises
    6 min
  9. 9

    Boss: Validate Binary Search TreeBoss

    The island's final test: prove a tree is a BST. Every node must respect bounds set by ALL of its ancestors — checking children against parents is the trap. Carry (lo, hi) down, or verify the inorder walk climbs strictly.

    Complete codeDebugCode orderMultiple choiceTrace
    8 exercises
    10 min
AlgoFox Pro

Trees & Graphs is part of AlgoFox Pro

You can read the whole course outline here. The exercises are in the app, along with your streak, your mistake review, and the progress that carries across your devices.

Pro starts with a 3 day free trial. The app store decides who is eligible for an introductory offer, and a lapsed subscription does not get another one.

Download AlgoFox on the App StoreGet AlgoFox on Google Play

Already subscribed in the app? Sign in with the same account and Pro works here too. Quick Play and Battle are free on the web, and so is the first course in every subject.