Accepted Java Solution Using Linked List and the method in the first Version of the question


  • 0
    N
    public class Solution {
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            LinkedList<List<Integer>> ret = new LinkedList<List<Integer>>();
            if(root==null){
                return ret;
            }
            List<TreeNode> level = new ArrayList<TreeNode>();
            level.add(root);
            while(true){
                if(level==null||level.isEmpty()){
                    return ret;
                }
                List<Integer> currentLevel = new ArrayList<Integer>();
                List<TreeNode> nextLevel = new ArrayList<TreeNode>();
                for(TreeNode node:level){
                    currentLevel.add(node.val);
                    if(node.left!=null) nextLevel.add(node.left);
                    if(node.right!=null) nextLevel.add(node.right);
                }
                ret.addFirst(currentLevel);
                level = nextLevel;
            }
        }
    }

Log in to reply
 

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