Skip to content

Commit

Permalink
Time: 186 ms (68.64%), Space: 35 MB (80.28%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Oct 18, 2023
1 parent b84cadd commit 634dfa4
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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:
return self.getGoodNodes(root, float("-inf"))

def getGoodNodes(self, root: TreeNode, newMax: int) -> int:
if not root:
return 0

newMax = max(newMax, root.val)
return (1 if root.val >= newMax else 0) + self.getGoodNodes(root.left, newMax) + self.getGoodNodes(root.right, newMax)

0 comments on commit 634dfa4

Please sign in to comment.