Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b0e9507

Browse files
authored
Create Day-17_Count_Good_Nodes_in_Binary_Tree.py
1 parent e7c085c commit b0e9507

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# O(N) Time and Space.
2+
class Solution:
3+
def goodNodes(self, root: TreeNode) -> int:
4+
5+
def dfs(root: TreeNode, ans: list, maxm: float) -> int:
6+
if root is None:
7+
return
8+
maxm = max(root.val, maxm)
9+
if root.val >= maxm:
10+
ans.append(root.val)
11+
12+
dfs(root.left, ans, maxm)
13+
dfs(root.right, ans, maxm)
14+
15+
ans = []
16+
dfs(root, ans, float('-inf'))
17+
return len(ans)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /