Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
Two Approach:
Key to solve: BFS by ArrayList
Create a TreeNode list that including all the nodes for each level, terminate loop if list is empty
Build another Integer list that including value of each level, need to be clear() in each level
Traverse the Tree from top down order:
Traverse all TreeNodes from list, and Update list with the next level Temp TreeNode list,
Update Integer List by levels, and add into result list as well
When the loop was terminate that means we have reach the bottom level. BSF completed
Check out the detail in coding
public static ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
//Integer ArrayList
ArrayList<ArrayList<Integer>> res= new ArrayList<ArrayList<Integer>>();
if(root==null) return res;
//Sub Integer ArrayList
ArrayList<Integer> level=new ArrayList<Integer>();
//Tree ArrayList storing each level of TreeNode one by one
ArrayList<TreeNode> list=new ArrayList<TreeNode>();
list.add(root);
level.add(root.val);
while(!list.isEmpty()){
ArrayList<TreeNode> nextTemp=new ArrayList<TreeNode>();
res.add(new ArrayList<Integer>(level));
level.clear();
//do not forget to clear ArrayList up
//since the ArrayList we only store TreeNode of each single level, not mutil
for(TreeNode n:list) {
if (n.left != null) {
nextTemp.add(n.left);
level.add(n.left.val);
}
if (n.right != null) {
nextTemp.add(n.right);
level.add(n.right.val);
}
}
//System.out.println("test");
list=new ArrayList<TreeNode>(nextTemp);
}
return res;
}
Solution_2: BSF by Queue
Create a TreeNode Queue that including all the nodes for each level, terminate loop if Queue is empty
Build another Integer list inside loop that including value of each level
Traverse the Tree from top down order:
Use Two variable to record number of nodes for current level and next level
Update current level Node with next Level nodes when move to nextLevel
Traverse the number of nodes with current level from Queue, meanwhile, update Queue with next level nodes
Update Integer List by levels, add into result list as well.
When the loop was terminate that means we have reach the bottom level => Queue is empty. BSF completed
Check out the detail in coding
public static ArrayList<ArrayList<Integer>> levelOrderQueue(TreeNode root) {
ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
if(root==null ) return res;
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.add(root);
int currNode=1;
int nextNode=1;
while(!queue.isEmpty()){
currNode=nextNode;
nextNode=0;
ArrayList<Integer> level=new ArrayList<Integer>();
for(int i=0;i<currNode;i++){
TreeNode node=queue.poll();
if(node.left!=null){
queue.add(node.left);
nextNode++;
}
if(node.right!=null){
queue.add(node.right);
nextNode++;
}
level.add(node.val);
}
res.add(new ArrayList<Integer>(level));
}
return res;
}