From cfc78c3c57d5d9a8c79df710c36979700fd43064 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Fri, 17 Nov 2023 19:33:16 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update111.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0111.二叉树的最小深度.md | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index c4e55a07..b27df4a6 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -708,6 +708,49 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public int MinDepth(TreeNode root) +{ + if (root == null) return 0; + int left = MinDepth(root.left); + int right = MinDepth(root.right); + if (root.left == null && root.right != null) + return 1+right; + else if(root.left!=null && root.right == null) + return 1+left; + + int res = 1 + Math.Min(left, right); + return res; +} +``` +```C# +// 迭代 +public int MinDepth(TreeNode root) +{ + if (root == null) return 0; + int depth = 0; + var que = new Queue(); + que.Enqueue(root); + while (que.Count > 0) + { + int size = que.Count; + depth++; + for (int i = 0; i < size; i++) + { + var node = que.Dequeue(); + if (node.left != null) + que.Enqueue(node.left); + if (node.right != null) + que.Enqueue(node.right); + if (node.left == null && node.right == null) + return depth; + } + } + return depth; +} +```

From 0843f2282dbcc9597bf156870149632d80def198 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sat, 18 Nov 2023 09:48:26 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=20222.=E5=AE=8C=E5=85=A8=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E9=80=92=E5=BD=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0222.完全二叉树的节点个数.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index d54f9b85..ef6e88aa 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -867,6 +867,31 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public int CountNodes(TreeNode root) +{ + if (root == null) return 0; + var left = root.left; + var right = root.right; + int leftDepth = 0, rightDepth = 0; + while (left != null) + { + left = left.left; + leftDepth++; + } + while (right != null) + { + right = right.right; + rightDepth++; + } + if (leftDepth == rightDepth) + return (int)Math.Pow(2, leftDepth+1) - 1; + return CountNodes(root.left) + CountNodes(root.right) + 1; + +} +```

From eb0504e5305ae37f3f6f569ebde423206ea11b0f Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sun, 19 Nov 2023 08:49:19 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=20110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0110.平衡二叉树.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index c7df9c8f..41669bff 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -908,6 +908,31 @@ impl Solution { } } ``` +### C# +```C# +public bool IsBalanced(TreeNode root) +{ + return GetHeight(root) == -1 ? false : true; +} +public int GetHeight(TreeNode root) +{ + if (root == null) return 0; + int left = GetHeight(root.left); + if (left == -1) return -1; + int right = GetHeight(root.right); + if (right == -1) return -1; + int res; + if (Math.Abs(left - right) > 1) + { + res = -1; + } + else + { + res = 1 + Math.Max(left, right); + } + return res; +} +```