1 ms simple java solution


  • 0
    S
    public class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            rec(root, 0);
            return lists;
        }
        List<List<Integer>>  lists= new ArrayList<List<Integer>>();
        
        void rec(TreeNode n, int level) {
            if(n==null) {
                return;
            }
            if(lists.size()-1<level) {
                lists.add(new ArrayList<Integer>());
            }
            lists.get(level).add(n.val);
            rec(n.left, level+1);
            rec(n.right, level+1);
        }
    }
    

Log in to reply
 

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