← 返回 meta 的题目列表Binary Tree: Print Left Boundary Bottom-Up and Right Boundary Top-Down
类型:online_judge
Given the root of a binary tree, output an integer sequence that contains:
the left boundary nodes in bottom-up order;
the right boundary nodes in top-down order.
Boundary definition:
Left boundary: starting from root, at each level take the leftmost visible node (prefer left, otherwise right) until a leaf.
Right boundary: starting from root, at each level take the rightmost visible node (prefer right, otherwise left) until a leaf.
Notes:
If a node appears in both boundaries, de-duplicate it (output once).
Output boundary nodes only.
Constraints: number of nodes N <= 2*10^5.
Implement a function that returns the sequence.
Example
Input
7
1 2 3 4 5 6 7
Output
4 2 1 3 7