Java 1ms beats 89% using preorder


  • 0
    R
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> lists=new ArrayList<List<Integer>>();
        getLevels(lists,root,0);
        return lists;
    }
    public void getLevels(List<List<Integer>> lists, TreeNode root,int lvl){
        if(root==null) return;
        List<Integer> list=null;
        if(lists.size()==lvl) {
            list=new LinkedList<Integer>();
            list.add(root.val);
            lists.add(list);
        }
        else
        {
            list=lists.get(lvl);
            list.add(root.val);
        }
        getLevels(lists,root.left,lvl+1);
        getLevels(lists,root.right,lvl+1);
    }

Log in to reply
 

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