Skip to content

Commit 72da592

Browse files
authored
Create L1448.go
1 parent 155c765 commit 72da592

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Medium/L1448.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Medium
2+
3+
func goodNodes(root *TreeNode) int {
4+
cnt := 0
5+
if root == nil {
6+
return cnt
7+
}
8+
dfs(root, root.Val, &cnt)
9+
return cnt
10+
}
11+
12+
func dfs(root *TreeNode, val int, cnt *int) {
13+
if root == nil {
14+
return
15+
}
16+
17+
if root.Val >= val {
18+
*cnt = (*cnt) + 1
19+
}
20+
21+
max := val
22+
if val < root.Val {
23+
max = root.Val
24+
}
25+
26+
dfs(root.Left, max, cnt)
27+
dfs(root.Right, max, cnt)
28+
}

0 commit comments

Comments
 (0)