- Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathMinimum-Depth-of-Binary-Tree.cs
48 lines (45 loc) · 1.29 KB
/
Minimum-Depth-of-Binary-Tree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem;
namespace_111_Minimum_Depth_of_Binary_Tree
{
publicclassSolution
{
publicintMinDepth(TreeNoderoot)
{
if(root==null)return0;
varqueue=newQueue<TreeNode>();
queue.Enqueue(root);
intlayer=1;
while(queue.Count>0)
{
intsize=queue.Count;
//each tree layer
for(inti=0;i<size;i++)
{
varnode=queue.Dequeue();
if(node!=null)
{
if(node.left==null&&node.right==null)returnlayer;
if(node.left!=null)queue.Enqueue(node.left);
if(node.right!=null)queue.Enqueue(node.right);
}
}
layer++;
}
returnlayer;
}
}
publicclassTreeNode
{
publicintval;
publicTreeNodeleft;
publicTreeNoderight;
publicTreeNode(intval=0,TreeNodeleft=null,TreeNoderight=null)
{
this.val=val;
this.left=left;
this.right=right;
}
}
}