857. Boundary Traversal of Binary Tree

EasyTreesBinary TreeDFSTraversal

Given the root of a binary tree, return its boundary traversal: the root, then the left boundary top-down (excluding leaves), then all leaves left to right, then the right boundary bottom-up (excluding leaves). Each node appears once. The tree is given as a level-order array.

Input: A level-order array of the tree.

Output: Array — the boundary in traversal order.

Examples

Example 1
Input: [1,2,3,4,5,6,7]
Output: [1,2,4,5,6,7,3]
Explanation: Root, left edge, leaves, right edge reversed.
Example 2
Input: [1]
Output: [1]
Explanation: Single node.
Example 3
Input: [1,2,null,3]
Output: [1,2,3]
Explanation: Left-only spine.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →