Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4c14916

Browse files
Merge pull request youngyangyang04#2340 from eeee0717/master
Update111.二叉树的最小深度,添加C#版
2 parents 2e33887 + eb0504e commit 4c14916

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

‎problems/0110.平衡二叉树.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,31 @@ impl Solution {
908908
}
909909
}
910910
```
911+
### C#
912+
```C#
913+
public bool IsBalanced(TreeNode root)
914+
{
915+
return GetHeight(root) == -1 ? false : true;
916+
}
917+
public int GetHeight(TreeNode root)
918+
{
919+
if (root == null) return 0;
920+
int left = GetHeight(root.left);
921+
if (left == -1) return -1;
922+
int right = GetHeight(root.right);
923+
if (right == -1) return -1;
924+
int res;
925+
if (Math.Abs(left - right) > 1)
926+
{
927+
res = -1;
928+
}
929+
else
930+
{
931+
res = 1 + Math.Max(left, right);
932+
}
933+
return res;
934+
}
935+
```
911936

912937
<p align="center">
913938
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

‎problems/0111.二叉树的最小深度.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,49 @@ impl Solution {
708708
}
709709
}
710710
```
711+
### C#
712+
```C#
713+
// 递归
714+
public int MinDepth(TreeNode root)
715+
{
716+
if (root == null) return 0;
717+
int left = MinDepth(root.left);
718+
int right = MinDepth(root.right);
719+
if (root.left == null && root.right != null)
720+
return 1+right;
721+
else if(root.left!=null && root.right == null)
722+
return 1+left;
723+
724+
int res = 1 + Math.Min(left, right);
725+
return res;
726+
}
727+
```
728+
```C#
729+
// 迭代
730+
public int MinDepth(TreeNode root)
731+
{
732+
if (root == null) return 0;
733+
int depth = 0;
734+
var que = new Queue<TreeNode>();
735+
que.Enqueue(root);
736+
while (que.Count > 0)
737+
{
738+
int size = que.Count;
739+
depth++;
740+
for (int i = 0; i < size; i++)
741+
{
742+
var node = que.Dequeue();
743+
if (node.left != null)
744+
que.Enqueue(node.left);
745+
if (node.right != null)
746+
que.Enqueue(node.right);
747+
if (node.left == null && node.right == null)
748+
return depth;
749+
}
750+
}
751+
return depth;
752+
}
753+
```
711754

712755
<p align="center">
713756
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

‎problems/0222.完全二叉树的节点个数.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,31 @@ impl Solution {
867867
}
868868
}
869869
```
870+
### C#
871+
```C#
872+
// 递归
873+
public int CountNodes(TreeNode root)
874+
{
875+
if (root == null) return 0;
876+
var left = root.left;
877+
var right = root.right;
878+
int leftDepth = 0, rightDepth = 0;
879+
while (left != null)
880+
{
881+
left = left.left;
882+
leftDepth++;
883+
}
884+
while (right != null)
885+
{
886+
right = right.right;
887+
rightDepth++;
888+
}
889+
if (leftDepth == rightDepth)
890+
return (int)Math.Pow(2, leftDepth+1) - 1;
891+
return CountNodes(root.left) + CountNodes(root.right) + 1;
892+
893+
}
894+
```
870895

871896
<p align="center">
872897
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /