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 }
相关文章
- 12-21Binary Tree Postorder Traversal
- 12-21leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
- 12-21LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
- 12-21leetcode Binary Tree Inorder Traversal python
- 12-21LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
- 12-21[LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal
- 12-21Leetcode#145 Binary Tree Postorder Traversal
- 12-21[LeetCode145]Binary Tree Postorder Traversal
- 12-21[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal
- 12-21Binary Tree Inorder Traversal