cs: add “1448. Count Good Nodes in Binary Tree”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
7e9a865db7
commit
19bc5bb150
1 changed files with 25 additions and 0 deletions
25
cs/count-good-nodes-in-binary-tree.cs
Normal file
25
cs/count-good-nodes-in-binary-tree.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* public int val;
|
||||
* public TreeNode left;
|
||||
* public TreeNode right;
|
||||
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
private int GoodNodes(TreeNode root, int m) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int newMax = Math.Max(m, root.val);
|
||||
return (root.val >= m ? 1 : 0) + GoodNodes(root.left, newMax) + GoodNodes(root.right, newMax);
|
||||
}
|
||||
|
||||
public int GoodNodes(TreeNode root) => GoodNodes(root, int.MinValue);
|
||||
}
|
Loading…
Reference in a new issue