Boundary Traversal
Hardjavapythonccppjavascript
Print the boundary of a binary tree in anti-clockwise order, without repeating any node: the root, then the left boundary top-down (excluding leaves), then all leaves left-to-right, then the right boundary bottom-up (excluding leaves).
Input Format
The first line contains an integer M, the number of level-order tokens. The second line contains M space-separated tokens (integers or null).
Output Format
Print the boundary node values separated by single spaces.
Example 1
Input
7 1 2 3 4 5 6 7
Output
1 2 4 5 6 7 3
Explanation: Root 1, left boundary 2, leaves 4 5 6 7, right boundary 3.
- 1 <= number of nodes <= 5000
- -1000000000 <= node value <= 1000000000
Submit an attempt to unlock the solution. (Sign in first.)
Related Problems
- Serialize and Deserialize Binary TreeHard · java, python, c, cpp, javascriptSolve Problem
- Maximum Path Sum in Binary TreeHard · java, python, c, cpp, javascriptSolve Problem
- Vertical Order TraversalHard · java, python, c, cpp, javascriptSolve Problem