Open fockspaces opened 7 months ago
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def goodNodes(self, root: TreeNode) -> int: def DFS(root, cur_max): if not root: return 0 cur_max = max(cur_max, root.val) return DFS(root.left, cur_max) + DFS(root.right, cur_max) + (root.val >= cur_max) return DFS(root, root.val)