Skip to content

Commit 5fc0e7a

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Update 1448. Count Good Nodes in Binary Tree.py
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent 4e61cf1 commit 5fc0e7a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def goodNodes(self, root: TreeNode) -> int:
9+
def dfs(root: TreeNode, mx: int):
10+
if root is None:
11+
return
12+
nonlocal ans
13+
if mx <= root.val:
14+
ans += 1
15+
mx = root.val
16+
dfs(root.left, mx)
17+
dfs(root.right, mx)
18+
19+
ans = 0
20+
dfs(root, -1000000)
21+
return ans

0 commit comments

Comments
 (0)