From 19bc5bb1509a0e4c51749ca61fc18dd503ce0eda Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 7 Jan 2024 21:09:16 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=E2=80=9C1448.=20Count=20Good=20Nod?= =?UTF-8?q?es=20in=20Binary=20Tree=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/count-good-nodes-in-binary-tree.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 cs/count-good-nodes-in-binary-tree.cs diff --git a/cs/count-good-nodes-in-binary-tree.cs b/cs/count-good-nodes-in-binary-tree.cs new file mode 100644 index 0000000..20cbf43 --- /dev/null +++ b/cs/count-good-nodes-in-binary-tree.cs @@ -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); +}