This is the solution about Leetcode - Binary Tree Vertical Order Traversal.

Description

Given a binary tree, return the vertical order traversal of its nodes’ values. (ie, from top to bottom, column by column).

If two nodes are in the same row and column, the order should be from left to right.

Examples:

Given binary tree [3,9,20,null,null,15,7],
3
/\
/ \
9 20
/\
/ \
15 7
return its vertical order traversal as:

1
2
3
4
5
6
[
[9],
[3,15],
[20],
[7]
]

Given binary tree [3,9,8,4,0,1,7],

1
2
3
4
5
6
7
3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7

return its vertical order traversal as:

1
2
3
4
5
6
7
[
[4],
[9],
[3,0,1],
[8],
[7]
]

Given binary tree [3,9,8,4,0,1,7,null,null,null,2,5] (0’s right child is 2 and 1’s left child is 5),

1
2
3
4
5
6
7
8
9
10
11
12
13
14
3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7
/\
/ \
5 2
```
return its vertical order traversal as:

[
[4],
[9,5],
[3,0,1],
[8,2],
[7]
]

1
2
3
4
5
6
7
8
9
10
11
12
## Solution
### Analysis
#### Solution1: BFS
1. Use one Queue to traverse all nodes
2. Use another Queue to record its location
3. Use TreeMap to record the result.
#### Solution2: BFS
Similar solution with solution1, ues HashMap and range to instead of TreeMap.
It is more efficient. Because all operation in TreeMap is O(lgn).

/**

  • Definition for a binary tree node.
  • public class TreeNode {
  • int val;
  • TreeNode left;
  • TreeNode right;
  • TreeNode(int x) { val = x; }
  • }
    */

public class Solution1 {
public List> verticalOrder(TreeNode root) {
List> result = new ArrayList<>();
if(root==null) return result;
Queue locations = new LinkedList<>();
Queue nodes = new LinkedList<>();
Map> map= new TreeMap<>();
nodes.offer(root);
locations.offer(0);
while(!nodes.isEmpty()) {
TreeNode node = nodes.poll();
int location = locations.poll();
if(!map.containsKey(location)) {
map.put(location, new ArrayList<>());
}
map.get(location).add(node.val);

        if(node.left!=null) {
            locations.offer(location-1);
            nodes.offer(node.left);
        }
        if(node.right!=null) {
            locations.offer(location+1);
            nodes.offer(node.right);
        }
    }
    result = new ArrayList(map.values());
    return result;
}

}

public class Solution2 {
public List> verticalOrder(TreeNode root) {
List> result = new ArrayList<>();
if(root==null) return result;
Queue locations = new LinkedList<>();
Queue nodes = new LinkedList<>();
Map> map= new HashMap<>();
nodes.offer(root);
locations.offer(0);
int min = 0, max = 0;
while(!nodes.isEmpty()) {
TreeNode node = nodes.poll();
int location = locations.poll();
if(!map.containsKey(location)) {
map.put(location, new ArrayList<>());
}
map.get(location).add(node.val);

        if(node.left!=null) {
            locations.offer(location-1);
            nodes.offer(node.left);
            min = Math.min(min,location-1);
        }
        if(node.right!=null) {
            locations.offer(location+1);
            nodes.offer(node.right);
            max = Math.max(max,location+1);
        }
    }
    for(int i=min; i<=max; i++) {
        result.add(map.get(i));
    }
    return result;
}

}

```