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; +} +```
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