Infosys Vertical Order Tree Traversal
Infosys Vertical Order Tree Traversal
HardTrees100% accepted
Infosys parses XML config files as binary trees and needs a vertical column-by-column representation. Given the root of a binary tree, return the vertical order traversal: assign column 0 to root, left child gets col-1, right child gets col+1. For nodes in the same column and row, sort by value. Return lists of values column by column from leftmost to rightmost.
Approach
Return vertical order traversal of a binary tree sorted by column, row, then value.
Input
3
3 9 20 -1 -1 15 7
Output
[[9],[3,15],[20],[7]]
More Trees problems
Browse all coding problems →solution.py
Loading editor...
Test 1
Test 2
Test 3
Press Run to test your custom input, or Submit to judge against all test cases.
Sign in to continue
Create a free account to run code, submit solutions and track progress.
- Free forever — no credit card
- 10+ languages with live execution
- Points, streaks & leaderboards