-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo111_Tree.cs
66 lines (63 loc) · 2.51 KB
/
No111_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using LeetCode.ExtensionFunction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LeetCode_111
{
//static void Main(string[] args)
//{
// var solution = new Solution();
// while (true)
// {
// //int input = int.Parse(Console.ReadLine());
// //int input2 = int.Parse(Console.ReadLine());
// //int input3 = int.Parse(Console.ReadLine());
// //string input = Console.ReadLine();
// //string input2 = Console.ReadLine();
// //int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
// //int input2 = int.Parse(Console.ReadLine());
// int?[] data = new int?[] { 1, 2, 3, 4, 5 };
// //int?[] data = new int?[] { 1, 2, 2, 3, 3, null, null, 4, 4 };
// //int?[] data = new int?[] { 1, 2, null, 3 };
// //int?[] data = new int?[] { 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, null, null, 5, 5 };
// var tree = new DataStructureBuilder().BuildTree(data);
// var res = solution.MinDepth(tree);
// ConsoleX.WriteLine(res);
// }
//}
public class Solution
{
/// <summary>
/// 迭代,或者说的广度优先迭代,一旦找到叶子就返回,最高效的办法了。深度优先需要获得所有之后才能比较出最小,不是好办法
/// 时间复杂度:O(n),虽说都是O(n),但是即使在最差的情况下:平衡树,也只会访问n/2个节点
/// 空间复杂度:O(n),和时间复杂度一样的情况
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
public int MinDepth(TreeNode root)
{
int cur_level = 0;
Queue<TreeNode> queue = new Queue<TreeNode>();
if (root != null)
queue.Enqueue(root);
while (queue.Count > 0)
{
cur_level++;
foreach (var node in queue.ToList())
{
root = queue.Dequeue();
if (root == null)
continue;
else if (root.left == null && root.right == null)
return cur_level;
if (root.left != null)
queue.Enqueue(root.left);
if (root.right != null)
queue.Enqueue(root.right);
}
}
return cur_level;
}
}
}