LeetCode - Binary Tree Level Order Traversal

2 minute read

Problem description

description

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).

For example:

1
2
3
4
5
6
7
8
9
10
11
12
Given binary tree [3,9,20,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7
return its level order traversal as:
[
  [3],
  [9,20],
  [15,7]
]

Analysis

A simple BFS problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> q = new ArrayDeque<>();
        List<List<Integer>> res = new ArrayList<>();
        if (root == null){
            return res;
        }
        q.offer(root);
        while (!q.isEmpty()){
            List<Integer> list = new ArrayList<>();
            List<TreeNode> next = new ArrayList<>();
            while(!q.isEmpty()){
                TreeNode node = q.poll();
                list.add(node.val);
                if (node.left != null){
                    next.add(node.left);
                }
                
                if (node.right != null){
                    next.add(node.right);
                }
            }
            
            res.add(list);
            
            for (TreeNode node:next){
                q.offer(node);
            }
        }
        
        return res;
    }
}

In each iteration, we don’t need to use another array to store the next level. However, we can use q.size() to find out how many item this level will have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
  public List<List<Integer>> levelOrder(TreeNode root) {
    List<List<Integer>> levels = new ArrayList<List<Integer>>();
    if (root == null) return levels;

    Queue<TreeNode> queue = new LinkedList<TreeNode>();
    queue.add(root);
    int level = 0;
    while ( !queue.isEmpty() ) {
      // start the current level
      levels.add(new ArrayList<Integer>());

      // number of elements in the current level
      int level_length = queue.size();
      for(int i = 0; i < level_length; ++i) {
        TreeNode node = queue.remove();

        // fulfill the current level
        levels.get(level).add(node.val);

        // add child nodes of the current level
        // in the queue for the next level
        if (node.left != null) queue.add(node.left);
        if (node.right != null) queue.add(node.right);
      }
      // go to next level
      level++;
    }
    return levels;
  }
}

What to improve

  • standard BFS use q.size() for iteration.