← 返回 bytedance 的题目列表Binary Tree Side View Variant
类型:online_judge
Binary Tree Left/Right Side View Variant
Given the root of a binary tree root, output (top-down) the visible node values from the left side and from the right side for each level.
Left visible: the leftmost node in that level
Right visible: the rightmost node in that level
If a level contains only one node, that node is both left- and right-visible.
Input
Line 1: integer n, the number of nodes in level-order representation.
Line 2: n tokens in level-order; null represents a missing node.
Output
Print two lines:
Left-visible sequence (space-separated)
Right-visible sequence (space-separated)
Constraints
0 <= n <= 2 * 10^5
Node values fit in 32-bit signed int
Target time O(n), extra space O(w) where w is max width
Example
Input:
7
1 2 3 null 5 null 4
Output:
1 2 5
1 3 4
Example
Input
7
1 2 3 null 5 null 4
Output
1 2 5
1 3 4