Solution might be to use min less in for loops conditions and replace it with the if condition like you have it. Surprisingly, I've seen so many solutions that use the min in for loops.

Very clever using a map<TreeNode, Integer> to store previous node's indexes. Spent quite some time working around this issue. Should have come up with this straightforward approach! up voted

@siriux Big O is not the best way to measure stuff. As unordered_map would give you O(N) when there is not enough memory. It is way more cost in memory compare with map. I understand it is faster when you have sufficient memory, but map has a really good performance when the tree is not big.
so the key here is not using map or unordered_map, it is get the idea right and have an understanding of ups and downs in each datastructures. If you get that, you passed this problem.

from collections import defaultdict
class Solution(object):
def verticalOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
cols = defaultdict(list)
s = list()
if root:
s.append((root, 0))
while s:
node, col = s.pop(0)
cols[col].append(node.val)
if node.left:
s.append((node.left, col - 1))
if node.right:
s.append((node.right, col + 1))
return [cols[k] for k in sorted(cols.keys())]

Hey, if you are asking about this line tree[i][j] = new ArrayList<Integer>(2);, then it doesn't really prevent a collision, it's just the case of over-optimization (the JVM is usually fast enough to find enough space for an ArrayList, however by default it preallocates space for 10 elements, which is too much for us, so by making it preallocate only for 2 elements we are making its job easier - there are many more consecutive blocks of memory of length 2 (32-bit 'slots')). It is true that we traverse from left to right in DFS, so we add the left number first and then the right one if collision happens. Then in this line res.get(i).addAll(tree[i][j]); we simply copy the array to the result (left-to-right order). Let me know if you have any more questions :)

Up voting it.
I don't like obfuscated code (one liner or two liner) this is best and easy to understand solution
Very beautiful and elegant solution, congratulations.

I think the tree should like this, that 9, 10 and 2 are in the same vertical line, 11, 12 and 1 are in the same vertical line, while 13, 14 and 3 are in the same vertical line. This is my intuition for build a tree.

@fentoyal Great DFS solution! Sharing some thinkings on when to use BFS or DFS

If the tree is balanced, use DFS or BFS

If not balanced, use BFS, because DFS use bigger stack, and preallocated 2D matrix will have many entries empty (I am not talking about your solution, I am talking about others' solution).