Very simple java solution using one queue(2ms)


  • 0
    H
    public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
            if (root == null) return res;
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            
            while(!queue.isEmpty()) {
                int size = queue.size();
                List<Integer> temp = new ArrayList<>(size);
                while(size-- > 0) {
                    TreeNode node = queue.poll();            
                    temp.add(node.val);
                    if (node.left != null) queue.add(node.left);
                    if (node.right != null) queue.add(node.right);
                }
                res.add(temp);
            }
            
            return res;
        }
    

Log in to reply
 

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