import
java.util.ArrayList;
import
java.util.Stack;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public
class
Solution {
public
ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer> > result=
new
ArrayList<ArrayList<Integer> >();
if
(pRoot==
null
)
return
result;
Stack<TreeNode> s1=
new
Stack<TreeNode>();
Stack<TreeNode> s2=
new
Stack<TreeNode>();
s1.push(pRoot);
TreeNode temp;
ArrayList<Integer> tArrayList;
while
(!s1.empty()||!s2.empty()){
tArrayList=
new
ArrayList<Integer>();
while
(!s1.empty()){
temp=s1.pop();
tArrayList.add(temp.val);
if
(temp.left!=
null
)
s2.push(temp.left);
if
(temp.right!=
null
)
s2.push(temp.right);
}
if
(!tArrayList.isEmpty()){
result.add(tArrayList);
}
tArrayList=
new
ArrayList<Integer>();
while
(!s2.empty()){
temp=s2.pop();
tArrayList.add(temp.val);
if
(temp.right!=
null
)
s1.push(temp.right);
if
(temp.left!=
null
)
s1.push(temp.left);
}
if
(!tArrayList.isEmpty()){
result.add(tArrayList);
}
}
return
result;
}
}
|