Maximum Difference Between Node and Ancestor (LeetCode 1026): Solution and Explanation
Given a binary tree, find the largest |a.val - b.val| where node a is an ancestor of node b. The tree has at least two nodes.
Intuition
Comparing every node with every ancestor works but costs O(n * h). Look at it from the descendant's side instead: for a fixed node b, the ancestor that differs from it most is either the smallest or the largest value above it.
So each node only needs two numbers from above: the minimum and maximum on its root path. Pass them down the recursion as lo and hi.
Every ancestor and descendant pair lies on some root-to-leaf path, and the best pair on one path is just that path's max minus its min. When a path ends, return hi - lo, and keep the largest over all paths.
Approach: DFS carrying the path minimum and maximum
- Call
dfs(root, root.val, root.val). - At each node, set
lo = min(lo, node.val)andhi = max(hi, node.val). - At a null child, the path has ended, so return
hi - lo. - Return the larger of the results from the left and right subtrees.
Step-by-step example
root = [8,3,10,1,6,null,14,null,null,4,7,13]
- Start at 8 with lo = 8, hi = 8.
- Go to 3: lo = 3, hi = 8. Go to 1: lo = 1, hi = 8. 1 is a leaf, so this path gives 8 - 1 = 7.
- Back to 3, then 6 and its leaves 4 and 7: those paths span [3, 8], giving 5.
- Go right to 10 and 14, then 13: the path 8, 10, 14, 13 spans [8, 14], giving 6.
- The widest span is 7, from 8 down to 1. Return 7.
Code: Python, Java, C++
1class Solution:
2 def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:
3 def dfs(node, lo, hi):
4 if not node:
5 return hi - lo
6 lo = min(lo, node.val)
7 hi = max(hi, node.val)
8 return max(dfs(node.left, lo, hi), dfs(node.right, lo, hi))
9
10 return dfs(root, root.val, root.val)1class Solution {
2 public int maxAncestorDiff(TreeNode root) {
3 return dfs(root, root.val, root.val);
4 }
5
6 private int dfs(TreeNode node, int lo, int hi) {
7 if (node == null) return hi - lo;
8 lo = Math.min(lo, node.val);
9 hi = Math.max(hi, node.val);
10 return Math.max(dfs(node.left, lo, hi), dfs(node.right, lo, hi));
11 }
12}1class Solution {
2 int dfs(TreeNode* node, int lo, int hi) {
3 if (!node) return hi - lo;
4 lo = min(lo, node->val);
5 hi = max(hi, node->val);
6 return max(dfs(node->left, lo, hi), dfs(node->right, lo, hi));
7 }
8
9public:
10 int maxAncestorDiff(TreeNode* root) {
11 return dfs(root, root->val, root->val);
12 }
13};Time and space complexity
O(n)
Each node is visited once and does constant work.
O(h)
The recursion stack holds one root-to-node path, where h is the tree height (up to n for a skewed tree).
Other approaches
Compare each node with every ancestor (time O(n * h), space O(h))
Keep the current root path in a list and, at each node, compute the difference with every value on it. Simple but repeats work.
Bottom-up subtree min and max (time O(n), space O(h))
Have each call return the min and max of its subtree, and at every node compare node.val against both. This answers the same question from the ancestor's side.
Common mistakes
- Comparing a node only with its parent. The best pair can be many levels apart, like 8 and 1 in Example 1.
- Taking the global max minus the global min of the whole tree. Those two nodes may sit in different subtrees, so neither is an ancestor of the other.
- Using a shared lo and hi that are not restored on the way back up, which leaks one branch's values into its sibling.
Frequently asked questions
Why is it enough to track only min and max?
For a fixed descendant value x, |a - x| over ancestor values a is largest at one of the extremes. And max - min of a path already covers the pair of extremes themselves.
Do I need the absolute value anywhere?
No. hi is always at least lo, so hi - lo is already non-negative.
Can the recursion overflow for 5000 nodes?
A fully skewed tree reaches depth 5000. That is fine in most languages, but in Python you may want sys.setrecursionlimit or an explicit stack.