← 返回 snowflake 的题目列表Boundary Traversal of a Full Complete Balanced Binary Tree (Variant)
类型:online_judge
Problem: Boundary Traversal of a Full, Complete, Balanced Binary Tree (Variant)
Given the root root of a binary tree. The tree is guaranteed to be:
Full: each node has either 0 or 2 children
Complete: all levels are full except possibly the last, which is filled left to right
Balanced: for every node, left/right subtree heights differ by at most 1
Return the boundary traversal in anti-clockwise order:
Root (if non-empty)
Left boundary (excluding leaves): starting from root.left, go as-left-as-possible down
All leaves from left to right
Right boundary (excluding leaves): starting from root.right, go as-right-as-possible down, then add nodes bottom-up
Each node must appear at most once in the output.
Input Format
Level-order representation:
Line 1: integer n
Line 2: n tokens: integer values or null
Input is guaranteed to satisfy the full/complete/balanced constraints.
Output Format
Print boundary values separated by spaces.
Constraints
0 <= n <= 2 * 10^5
Node values are 32-bit integers
Example
Input:
7
1 2 3 4 5 6 7
Output:
1 2 4 5 6 7 3
Example
Input
0
Output