-
Notifications
You must be signed in to change notification settings - Fork 148
/
PrintFromTopToBottom23.java
78 lines (69 loc) · 1.74 KB
/
PrintFromTopToBottom23.java
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
package com.so;
import java.util.ArrayList;
import java.util.LinkedList;
import com.so.Common.TreeNode;
import java.util.Queue;
/**
* 第23题
* 层序遍历二叉树
*
* @author qgl
* @date 2017/08/11
*/
public class PrintFromTopToBottom23 {
/**
* 解法一:迭代
* 时间复杂度:O(n),空间复杂度:O(n)
*
* @param root
* @return
*/
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
list.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
return list;
}
/**
* 解法二:递归
* 时间复杂度:O(n),空间复杂度:O(n)
*
* @param root
* @return
*/
public ArrayList<Integer> PrintFromTopToBottom2(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
if (root == null) {
return list;
}
list.add(root.val);
levelOrder(root, list);
return list;
}
public void levelOrder(TreeNode root, ArrayList<Integer> list) {
if (root == null) {
return;
}
if (root.left != null) {
list.add(root.left.val);
}
if (root.right != null) {
list.add(root.right.val);
}
levelOrder(root.left, list);
levelOrder(root.right, list);
}
}