-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathLevelOrderTraversal.cs
87 lines (79 loc) · 2.59 KB
/
LevelOrderTraversal.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* ==============================================================================
* 功能描述:LevelOrderTraversal
* 创 建 者:gz
* 创建日期:2017/4/19 12:23:31
* ==============================================================================*/
using System.Collections.Generic;
using System.Linq;
//Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
//For example:
//Given binary tree [3,9,20,null,null,15,7],
// 3
// / \
// 9 20
// / \
// 15 7
//return its bottom-up level order traversal as:
//[
// [15,7],
// [9,20],
// [3]
//]
namespace Tree.TreeLib
{
/// <summary>
/// 107. Binary Tree Level Order Traversal II
/// </summary>
public class LevelOrderTraversal
{
public IList<IList<int>> LevelOrderBottom(TreeNode root)
{
var rtn = LevelOrderUp(root);
if (rtn != null && rtn.Count > 0)
{
List<IList<int>> rtn2 = new List<IList<int>>();
rtn2.AddRange(rtn);
rtn2.Reverse(); //Ilist.Reverse() failure!
return rtn2;
}
return new List<IList<int>>();
}
public IList<IList<int>> LevelOrderUp(TreeNode root)
{
if (root == null)
return new List<IList<int>>();
IList<IList<int>> rtn = new List<IList<int>>{new List<int> {root.val}};
var tmp = getNextLevel(new List<TreeNode> { root });
if (tmp != null && tmp.Count > 0)
{
foreach (var item in tmp)
rtn.Add(item);
}
return rtn;
}
private IList<IList<int>> getNextLevel(IList<TreeNode> curLevel)
{
if (curLevel == null || curLevel.Count == 0)
return null;
IList<IList<int>> rtn= new List<IList<int>>();
List<TreeNode> nextn = new List<TreeNode>();
foreach (var item in curLevel)
{
if (item.left != null)
nextn.Add(item.left);
if (item.right != null)
nextn.Add(item.right);
}
if (nextn.Count == 0)
return null;
rtn.Add(nextn.Select(r => r.val).ToList());
var children = getNextLevel(nextn);
if (children != null && children.Count > 0)
{
foreach (var item in children)
rtn.Add(item);
}
return rtn;
}
}
}