Top View of a Binary Tree
Updated
Problem statement
You are given the root of a binary tree. Give every node a column: the root is in column 0, a left child is one column to the left of its parent (column minus 1), and a right child is one column to the right (column plus 1).
Looking at the tree from above, in each column you see only its highest node, the one closest to the root. If several nodes in a column are equally high, you see the one that comes first from left to right on that level. Return the values you see, from the leftmost column to the rightmost.
Function
topView(root: TreeNode) → List<Integer>Examples
Example 1
root = [1,2,3,null,4,null,5,null,6]return = [2,1,3,5]Columns from left to right: 2 is alone in column -1; column 0 has 1 on top (4 is below it); column 1 has 3 on top (6 is below it); column 2 has 5. Answer: [2,1,3,5].
Example 2
root = [7]return = [7]One node: [7].
Example 3
root = [10,20,30,null,40,null,null,null,50]return = [20,10,30]Node 50 is in column 1 (left, right, right from the root), but 30 is higher in that column, so 50 is hidden. 40 is hidden under 10. Answer: [20,10,30].
Constraints
- The tree has between
1and500nodes. -10^4 <= node.val <= 10^4(values may repeat)
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Decrypt a String from Binary Tree ColumnsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Transform and Prune a Mode-Valued Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions
- Path Between Two Binary-Tree NodesSnowflake · PHONE SCREENSnowflake interview questions
- Count LevelsBlackRock · OABlackRock interview questions
- Vertical Order Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Binary Tree Vertical Order with Stable TiesMeta · ONSITE INTERVIEWMeta interview questions
- Top View of a Binary TreeInMobi · ONSITE INTERVIEWInMobi interview questions