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.*;
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(null==pRoot)
return result;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(pRoot);
ArrayList<Integer> arr = new ArrayList<Integer>();
boolean isoushu = false;
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0;i<size;i++){
TreeNode temp = queue.poll();
arr.add(temp.val);
if(temp.left!=null)
queue.offer(temp.left);
if(temp.right!=null)
queue.offer(temp.right);
}
if(isoushu){
Collections.reverse(arr);
isoushu=!isoushu;
}
result.add(arr);
arr= new ArrayList<Integer>();
}
return result;
}
}
网友评论