Skip to content

Commit d1c71ec

Browse files
authored
Merge pull request #573 from notauserx/1448-Count-Good-Nodes-in-Binary-Tree-csharp
Create 1448-Count-Good-Nodes-in-Binary-Tree.cs
2 parents c70be8f + 244bf78 commit d1c71ec

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
14+
public class Solution {
15+
private int goodNodeCount = 0;
16+
public void dfs(TreeNode cur, int pathMax) {
17+
if(cur == null) return;
18+
if(cur.val >= pathMax) {
19+
pathMax = cur.val;
20+
goodNodeCount++;
21+
}
22+
dfs(cur.left, pathMax);
23+
dfs(cur.right, pathMax);
24+
25+
}
26+
public int GoodNodes(TreeNode root) {
27+
dfs(root, int.MinValue);
28+
return goodNodeCount;
29+
}
30+
}

0 commit comments

Comments
 (0)