Java 1ms beats 98% using preorder


  • 7
    R
     public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> list=new ArrayList();
        get(list,root,0);
        Collections.reverse(list);
        return list;
    }
    public void get(List<List<Integer>> list,TreeNode root,int lvl){
        if(root==null) return;
        List<Integer> lvlList;
        if(lvl==list.size()){
            lvlList=new ArrayList<Integer>();
            list.add(lvlList);
        }
        lvlList=list.get(lvl);
        lvlList.add(root.val);
        get(list,root.left,lvl+1);
        get(list,root.right,lvl+1);
    }

Log in to reply
 

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