Top View of Binary Tree
Medium Problems DSA practice problem on Onlearn.
Difficulty: easy.
Topics: Top View of a Binary Tree, Binary Tree, Level Order Traversal, Queue, Map, Time Complexity, Space Complexity, level order traversal, queue, binary tree, hash map, bfs, tree traversal, Vertical Order Traversal.
Given a Binary Tree, return its Top View. The Top View of a Binary Tree is the set of nodes visible when we see the tree from the top. Input Specification: The input will be a level order traversal of the binary tree. A value of 1 indicates a null child. Output Specification: Return a list of integers representing the Top View of the binary tree, ordered from left to right based on their horizontal distance. Examples: Example 1: Input: 1 2 3 4 10 9 11 1 5 1 1 1 1 1 1 1 6 (Represents the tree: root 1, left 2, right 3; 2's left 4, right 10; 3's left 9, right 11; 4's right 5; 5's right 6) Output: [4, 2, 1, 3, 11] Explanation: When viewed from the top, nodes 4, 2, 1, 3, and 11 are visible. Example 2: Input: 2 7 5 2 6 1 9 1 1 5 11 4 1 Output: [2, 7, 2, 5, 9] Explanation: When viewed from the top, nodes 2, 7, 2 (another node with value 2), 5, and 9 are visible.