Leetcode (Vertical Traversal of Tree)


For TC - [0,2,1,3,null,null,null,4,5,null,7,6,null,10,8,11,9]
My O/P - [[4,10,11],[3,7,6],[2,5,8,9],[0],[1]]
Expected - [[4,10,11],[3,6,7],[2,5,8,9],[0],[1]]

Can someone explain why it should be [3,6,7] ? In TC, 7 comes before 6

If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.

treeleetcode

Here, nodes with touching sides have same co-ordinates.

So, acc to you should it be [3,6,7] or [3,7,6] ?

position of 3 = 0 - 1-1 , 0-1-1 => -2,-2
position of 6 = 0 -1-1+1-1 , 0 -1 -1 -1 -1 => -2,-4
position of 7 = 0 -1 -1 -1 +1 , 0 -1 -1 -1 -1 => -2,-4

they are in same block because value of x is same . 3 comes first because the value of y of 3 is highest (-2) . 6 comes second because 6 is smaller than 7

2 Likes

Since 6 and 7 have the same co-ordinates and 6<7, it should be [3,6,7]

2 Likes