Maximum Difference Between Node and Ancestor

Given therootof a binary tree, find the maximum valueVfor which there existsdifferentnodesAandBwhereV = |A.val - B.val| andAis an ancestor ofB.

(A node A is an ancestor of B if either: any child of A is equal to B, or any child of A is an ancestor of B.)

Example 1:

Note:

DFS

一条路上记录最大和最小即可

Last updated

Was this helpful?