LeetCode – Binary Tree Vertical Order Traversal (Java)

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

Java Solution

For each node, its left child's degree is -1 and is right child's degree is +1. We can do a level order traversal and save the degree information.

public List<List<Integer>> verticalOrder(TreeNode root) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    if(root==null)
        return result;
 
    // level and list    
    HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
 
    LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
    LinkedList<Integer> level = new LinkedList<Integer>();
 
    queue.offer(root);
    level.offer(0);
 
    int minLevel=0;
    int maxLevel=0;
 
    while(!queue.isEmpty()){
        TreeNode p = queue.poll();
        int l = level.poll();
 
        //track min and max levels
        minLevel=Math.min(minLevel, l);
        maxLevel=Math.max(maxLevel, l);
 
        if(map.containsKey(l)){
            map.get(l).add(p.val);
        }else{
            ArrayList<Integer> list = new ArrayList<Integer>();
            list.add(p.val);
            map.put(l, list);
        }
 
        if(p.left!=null){
            queue.offer(p.left);
            level.offer(l-1);
        }
 
        if(p.right!=null){
            queue.offer(p.right);
            level.offer(l+1);
        }
    }
 
 
    for(int i=minLevel; i<=maxLevel; i++){
        if(map.containsKey(i)){
            result.add(map.get(i));
        }
    }
 
    return result;
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Matias SM

    Same idea, alternate implementation (recursive). Note: I print the result, but returning a level list as shown in the example is trivially done with the same logic.

    Note: this is also a Time O(N) impl and Space O(N).
    Note 2: recursive implementation IMHO is way cleaner and will probably require less memory since we don’t have extra lists. Stack size shouldn’t be an issue considering that it only requires O(H) recursions (H = height of the tree)


    class BinTreeNode {
    public final T value;
    public BinTreeNode left;
    public BinTreeNode right;
    }

    void columnOrder(BinTreeNode n) {
    Map<Integer, List<BinTreeNode>> columns = new HashMap();
    columnOrder(n , columns, 0);
    for (Integer idx : columns.keySet().stream().sorted().collect(Collectors.toList())) {
    List<BinTreeNode> column = columns.get(idx);
    for (BinTreeNode c : column) {
    System.out.println(c.value);
    }
    }
    }

    void columnOrder(BinTreeNode n, Map<Integer, List<BinTreeNode>> columns, int column)
    {
    List<BinTreeNode> l = columns.computeIfAbsent(column, i -> new LinkedList());
    l.add(n);
    if (n.left != null) columnOrder(n.left , columns, column - 1);
    if (n.right != null) columnOrder(n.right , columns, column + 1);
    }