-
Notifications
You must be signed in to change notification settings - Fork 0
/
59.java
73 lines (68 loc) · 1.97 KB
/
59.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
import java.util.ArrayList;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.Stack;
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
if(pRoot==null) return result;
//定义层次
int layer=1;
//定义两个Stack分别存储奇数层和偶数层的节点
Stack<TreeNode> stack1=new Stack<TreeNode>();
stack1.push(pRoot);
Stack<TreeNode> stack2=new Stack<TreeNode>();
while(!stack1.empty() || !stack2.empty())
{
//偶数层
if(layer%2!=0)
{
ArrayList<Integer> tmp=new ArrayList<Integer>();
while(!stack1.empty())
{
TreeNode node=stack1.pop();
if(node!=null)
{
tmp.add(node.val);
stack2.push(node.left);
stack2.push(node.right);
}
}
if(!tmp.isEmpty())
{
result.add(tmp);
layer++;
}
}
//奇数层
else
{
ArrayList<Integer> tmp=new ArrayList<Integer>();
while(!stack2.empty())
{
TreeNode node=stack2.pop();
if(node!=null)
{
tmp.add(node.val);
stack1.push(node.right);
stack1.push(node.left);
}
}
if(!tmp.isEmpty())
{
result.add(tmp);
layer++;
}
}
}
return result;
}
}