Binary Tree Level Order Traversal II 從底部倒著輸出二叉樹每一層的元素
阿新 • • 發佈:2019-01-07
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
注意,Collections中有reverse方法
Collections.reverse(xx);
Reverses the order of the elements in the specified list.public class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> outter=new ArrayList<List<Integer>>();//跟下邊的寫法都可以 //LinkedList<List<Integer>> outter=new LinkedList<List<Integer>>(); while(root==null) return outter; //List<TreeNode> Node=new LinkedList<TreeNode>();//而對於LinkedList竟然不可以,是因為List沒有poll,linkedlist有poll LinkedList<TreeNode> Node=new LinkedList<TreeNode>(); //因為在下邊實現的queue的功能,所以可以直接寫成下行,能用queue就用queue吧,高大上一點 //Queue<TreeNode> Node=new LinkedList<TreeNode>(); Node.add(root); int outtercount=1; int innercount=0; List<Integer> inner=new ArrayList<Integer>(); inner.add(root.val); outter.add(inner); inner=new LinkedList<Integer>(); //while(Node!=null){ 就因為這兒的判斷語句寫成這個樣子,報錯多少次,真是 while(!Node.isEmpty()){ TreeNode node=Node.poll(); --outtercount; if(node.left!=null){ Node.add(node.left); inner.add(node.left.val); ++innercount; } if(node.right!=null){ Node.add(node.right); inner.add(node.right.val); ++innercount; } if(outtercount==0){ outtercount=innercount; innercount=0; if(!inner.isEmpty()){ outter.add(0,inner); inner=new LinkedList<Integer>(); } } } return outter; } }下邊的這個寫法主要是來體會一下Collections中的reverse方法 public class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> outter=new ArrayList<List<Integer>>(); //LinkedList<List<Integer>> outter=new LinkedList<List<Integer>>(); while(root==null) return outter; //LinkedList<TreeNode> Node=new LinkedList<TreeNode>(); //LinkedList<TreeNode> Node=new LinkedList<TreeNode>(); Queue<TreeNode> Node=new LinkedList<TreeNode>(); Node.add(root); int outtercount=1; int innercount=0; List<Integer> inner=new ArrayList<Integer>(); inner.add(root.val); outter.add(inner); inner=new LinkedList<Integer>(); //while(Node!=null){ while(!Node.isEmpty()){ TreeNode node=Node.poll(); --outtercount; if(node.left!=null){ Node.add(node.left); inner.add(node.left.val); ++innercount; } if(node.right!=null){ Node.add(node.right); inner.add(node.right.val); ++innercount; } if(outtercount==0){ outtercount=innercount; innercount=0; if(!inner.isEmpty()){ outter.add(inner); inner=new LinkedList<Integer>(); } } } Collections.reverse(outter); return outter; } }把第一種寫法和這個對比一下,就是稍微做了一點處理,大同小異 public class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> outter=new ArrayList<List<Integer>>(); while(root==null) return outter; Queue<TreeNode> Node=new LinkedList<TreeNode>(); Node.add(root); int outtercount=1; int innercount=0; List<Integer> inner=new ArrayList<Integer>(); while(!Node.isEmpty()){ TreeNode node=Node.poll(); --outtercount; inner.add(node.val); if(node.left!=null){ Node.add(node.left); ++innercount; } if(node.right!=null){ Node.add(node.right); ++innercount; } if(outtercount==0){ outtercount=innercount; innercount=0; outter.add(inner); inner=new LinkedList<Integer>(); } } Collections.reverse(outter); return outter; } }