Easy Java solution, beats 90%


  • 0
    M
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> sols = new ArrayList<>();
            if(root==null) return sols;
            levelOrder(root,0,sols);
            return sols;
        }
        public void levelOrder(TreeNode root, int lev, List<List<Integer>> sols){
            if(root==null) return;
            if(lev==sols.size())
                sols.add(new LinkedList<Integer>());
            sols.get(lev).add(root.val);
            levelOrder(root.left,lev+1,sols);
            levelOrder(root.right,lev+1,sols);
            
        }

Log in to reply
 

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