DSA
    advanced

    DP on Trees

    Software Engineer path · lesson 39 of 99
    time O(n)
    space O(n)

    Up to 15 slots (4 levels), numbers −99 to 99; children of slot i are 2i+1 and 2i+2.

    Try:
    -10920157
    SolvingSolvedBest bend

    Solve children before parents (post-order). Each node reports its best downward path.

    Code (running line highlighted)
    1. 1solve(node): # post-order: children first
    2. 2 if node is empty: return 0
    3. 3 L = max(0, solve(left)); R = max(0, solve(right)) # drop negative branches
    4. 4 best = max(best, node.val + L + R) # path that bends here
    5. 5 return node.val + max(L, R) # path that continues up
    Nodes solved
    0 / 5
    Invariant: down[c] is final for every finished child c.

    Best so far: —

    Tap any element in the diagram to inspect its state at this step.

    1 / 7

    Keys: ← → step, Space play/pause. Predict mode pauses before key steps and asks what happens next.

    Maximum independent set on a tree

    Sequence

    Order the steps of the take/skip tree DP.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    In "Binary Tree Maximum Path Sum", why must we return a different value than we update?

    The short version

    Imagine a company org chart. To calculate the total bonus pool, each manager sums their team's bonuses and passes the total up. DP on trees computes values bottom-up from leaves to root.

    In more depth

    Bottom-Up Aggregation

    Tree DP processes nodes in post-order: handle children first, then compute parent's value.

    Common Pattern:

    def dfs(node):
        if not node: return base_case
        left = dfs(node.left)
        right = dfs(node.right)
        # Update global answer using both subtrees
        global_ans = max(global_ans, f(left, right, node))
        # Return single-path value to parent
        return g(left, right, node)

    Path vs Subtree:

    Path: Can only extend through ONE child
    Subtree: Can aggregate both children

    Rerooting:

    For "answer rooted at each node", compute once from a root, then propagate to children using parent info.

    Key Gotchas:

    Global max vs return value are often different
    Handle null children carefully
    Consider negative values (sometimes skipping is better)

    When to reach for this

    • Find maximum/minimum value involving tree paths
    • Aggregate values from subtrees
    • Each node makes a choice based on children
    • Keywords: 'path in tree', 'subtree sum', 'tree diameter'

    Interview Questions

    Facebook
    Amazon
    Microsoft
    Google
    Uber
    Facebook
    Netflix
    Airbnb
    Bloomberg
    Two Sigma
    Palantir

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required