-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathPrintTree2.cs
114 lines (102 loc) · 3.45 KB
/
PrintTree2.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
题目名称:
把二叉树打印成多行
题目描述:
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行
代码结构:
class Solution
{
public List<List<int>> Print(TreeNode pRoot)
{
// write code here
}
}
*/
using System;
using System.Collections.Generic;
namespace PrintTree2 {
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode (int x)
{
val = x;
}
}
class Solution {
/// <summary>
/// 解法1,层次遍历
/// 基本思路:
/// 利用一个辅助队列,队列中依次保存二叉树每一层的所有节点。
/// </summary>
public List<List<int>> Print(TreeNode pRoot)
{
List<List<int>> lists = new List<List<int>>();
Queue<TreeNode> queue = new Queue<TreeNode>();
queue.Enqueue(pRoot);
while(queue.Count > 0){
int count = queue.Count;
List<int> list = new List<int>();
for(int i = 0; i < count; i ++){
TreeNode node = queue.Dequeue();
if(node != null){
list.Add(node.val);
queue.Enqueue(node.left);
queue.Enqueue(node.right);
}
}
if(list.Count > 0){
lists.Add(list);
}
}
return lists;
}
/// <summary>
/// 解法2,递归
/// 基本思路:
/// 对二叉树进行先序遍历,即先根节点,再左节点,再右节点。保证每一层是从左到右顺序。
/// 递归时利用depth记录二叉树的深度,即通过depth判断节点应该被加入到哪一层(lists[depth - 1])
/// </summary>
public void Print2Impl(TreeNode node, int depth, List<List<int>> lists){
if(node == null){
return;
}
if(depth > lists.Count){
lists.Add(new List<int>());
}
lists[depth - 1].Add(node.val);
Print2Impl(node.left, depth + 1, lists);
Print2Impl(node.right, depth + 1, lists);
}
public List<List<int>> Print2(TreeNode pRoot)
{
List<List<int>> lists = new List<List<int>>();
Print2Impl(pRoot, 1, lists);
return lists;
}
public void Dump(List<List<int>> lists) {
foreach(List<int> list in lists){
foreach(int i in list){
Console.Write(i + " ");
}
Console.WriteLine();
}
}
public void Test() {
TreeNode pRoot = null;
pRoot = new TreeNode(0);
pRoot.left = new TreeNode(1);
pRoot.left.left = new TreeNode(2);
pRoot.left.right = new TreeNode(3);
pRoot.right = new TreeNode(4);
pRoot.right.left = new TreeNode(5);
pRoot.right.right = new TreeNode(6);
pRoot.right.right.left = new TreeNode(7);
pRoot.right.right.right = new TreeNode(8);
// Dump(Print(pRoot));
Dump(Print2(pRoot));
}
}
}