Maximum Difference Between Node and Ancestor - Problem

You are given the root of a binary tree, and your mission is to find the maximum absolute difference between any ancestor-descendant pair in the tree.

An ancestor of a node is any node that lies on the path from the root to that node (including the root itself). A descendant is any node in the subtree rooted at a given node.

Goal: Find the maximum value v where v = |a.val - b.val| and node a is an ancestor of node b.

Example: If you have a path from root to leaf like 8 → 3 → 1, you can compare 8 with 3, 8 with 1, and 3 with 1 to find the maximum difference.

Input & Output

example_1.py — Basic Tree
$ Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
Output: 7
💡 Note: The maximum difference is between node 8 (ancestor) and node 1 (descendant), giving |8-1| = 7. Other possible differences include |3-1| = 2, |8-14| = 6, but 7 is the maximum.
example_2.py — Linear Tree
$ Input: root = [1,null,2,null,0,3]
Output: 3
💡 Note: The tree forms a path 1→2→0→3. The maximum difference is |0-3| = 3. Note that 1 and 0 are also ancestor-descendant with difference |1-0| = 1, and 2 and 3 give |2-3| = 1.
example_3.py — Single Node
$ Input: root = [5]
Output: 0
💡 Note: A single node tree has no ancestor-descendant pairs, so the maximum difference is 0.

Constraints

  • The number of nodes in the tree is in the range [2, 5000]
  • 0 ≤ Node.val ≤ 105
  • Note: Since we need at least one ancestor-descendant pair, minimum tree size is 2 nodes

Visualization

Tap to expand
GrandpaHeight: 8ftDadHeight: 3ftUncleHeight: 10ftChild1Height: 1ftChild2Height: 6ftCousinHeight: 14ftPath: Grandpa→Dad→Child1Min Height: 1ft, Max Height: 8ftDifference: 8 - 1 = 7ft (MAXIMUM!)This lineage has the greatest height spreadOther Paths:Grandpa→Dad→Child2: 8-3=5ftGrandpa→Uncle→Cousin: 14-8=6ftAll smaller than 7ft difference
Understanding the Visualization
1
Start the Journey
Begin at the family patriarch/matriarch (root) - they are both the tallest and shortest person initially
2
Meet Each Descendant
As you visit each family member down the lineage, update who is the tallest and shortest
3
Calculate Differences
At any point, the maximum height difference is simply tallest - shortest among people met so far
4
Explore All Branches
Visit every family branch (path) and track the overall maximum difference found
5
Return Maximum
The answer is the largest height difference found across all family lineages
Key Takeaway
🎯 Key Insight: For any root-to-leaf path, the maximum ancestor-descendant difference equals the difference between the highest and lowest values on that path. We only need one DFS traversal to track min/max values and find the answer!
Asked in
Amazon 35 Google 28 Meta 22 Microsoft 18
52.0K Views
Medium-High Frequency
~15 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen