[
tree
dfs
bf
]
Leetcode 1026. Maximum Difference Between Node and Ancestor
https://leetcode.com/problems/maximum-difference-between-node-and-ancestor
Let us traverse our tree from top to bottom with dfs
function, where we pass parameters:
node
is current node we visitinghigh
is maximum over all node values, which are above ournode
, that is the maximum among parent, parent of parent, parent of parent of parent and so on.low
is minumum over all node values, which are above ournode
.
No, we iterate over our tree and:
- Update our
self.Max
: look at two valuesabs(node.val - low)
andabs(node.val - high)
: it will be the biggest candidates for difference between node and its ancestor, wherenode
is lying in lower layer. - Now, we run our
dfs
for left and right children, where we updatedlow
andhigh
values: we need to include currentnode.val
.
We start with dfs(root, root.val, root.val)
, because we do not have nodes which are above our root.
Complexity: time complexity is O(n)
, space complexity is O(h)
, as any usual dfs have.
class Solution:
def maxAncestorDiff(self, root):
self.Max = 0
def dfs(node, low, high):
if not node: return
self.Max = max(self.Max, abs(node.val-low), abs(node.val-high))
low1, high1 = min(low, node.val), max(high, node.val)
dfs(node.left, low1, high1)
dfs(node.right, low1, high1)
dfs(root, root.val, root.val)
return self.Max
If you like the solution, you can upvote it on leetcode discussion section: Problem 1026