Binary Tree Level Order Traversal

Binary Tree Level Order Traversal
 1 public class Solution {
 2     public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
 3         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
 4         LinkedList<TreeNode> cur = new LinkedList<TreeNode>();
 5         if(root==null) return res;
 6         cur.offer(root);
 7         while(!cur.isEmpty()){
 8             LinkedList<TreeNode> next = new LinkedList<TreeNode>();
 9             ArrayList<Integer> sol = new ArrayList<Integer>();
10             for(TreeNode n :cur){
11                 sol.add(n.val);
12                 if(n.left!=null) next.offer(n.left);
13                 if(n.right!=null) next.offer(n.right);
14             }
15             res.add(sol);
16             cur = next;
17         }
18         return res;
19     }
20 }
View Code

Binary Tree Level Order Traversal

上一篇:【DB笔试面试615】在Oracle中,和谓词相关的查询转换有哪些?


下一篇:Binary Tree Inorder Traversal