#1026
Maximum Difference Between Node and Ancestor
pupil · 455 · lc medium +26 · verified · 78.1% accepted · 5,092 likes · top 89%
Description
Given the root of a binary tree, find the largest value v such that there exist two distinct nodes a and b with v = |a.val - b.val| and a is an ancestor of b.
Node a is an ancestor of b if every path from the root to b passes through a.
Example 1:
Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
Output: 7
Explanation: We have various ancestor-node differences, some of which are given below :
|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
Example 2:
Input: root = [1,null,2,null,0,3]
Output: 3
Code
1
2
3
4
5
6
7
8
9