Counting Maximal Value Roots in Binary Tree
# class Tree:
# def __init__(self, val, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def solve(self, root):
count = 0
def dfs(root):
if root:
left = dfs(root.left)
right = dfs(root.right)
if root.val >= left and root.val >= right:
nonlocal count
count += 1
return root.val
return max(left, right)
else:
return 0
dfs(root)
return count



TODO write preorder s postorder vs in order
Last updated