Faaez Razeen

Count Good Nodes in Binary Tree

  • 1 min read
  • LC-Medium
  • Binary Tree

3 years ago

Solution

TimeSpaceExplanation
O(n)O(log n)
def goodNodes(self, root: TreeNode) -> int: ans = 0 def dfs(root, max_in_path): nonlocal ans if root: if root.val >= max_in_path: ans += 1 max_in_path = max(max_in_path, root.val) dfs(root.left, max_in_path) dfs(root.right, max_in_path) dfs(root, -math.inf) return ans