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 cfc78c3

Browse files
committed
Update111.二叉树的最小深度,添加C#版
1 parent 3ffca33 commit cfc78c3

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎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">

0 commit comments

Comments
(0)

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