My Java Solution


  • 0
     public List<List<Integer>> levelOrderBottom(TreeNode root) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
    		if(root == null) return res;
    		Queue<TreeNode> queue = new LinkedList<TreeNode>();
    		Stack<List<Integer>> stack = new Stack<List<Integer>>();
    		queue.offer(root);
    		while(!queue.isEmpty()){
    			int size =queue.size();
    			List<Integer> temp = new ArrayList<Integer>();
    			while(size-- > 0){
    				root = queue.poll();
    				temp.add(root.val);
    				if(root.left != null) queue.offer(root.left);
    				if(root.right!= null) queue.offer(root.right);
    			}
    			stack.push(temp);
    		}
    		while(!stack.isEmpty()){
    			res.add(stack.pop());
    		}
    		return res; 
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.