Boundary of Binary Tree
Problem statement
Given a binary tree, return its anti-clockwise boundary: the root, the left boundary excluding leaves, every leaf from left to right, and the right boundary excluding leaves in bottom-up order.
Do not include any node more than once.
Function
boundaryOfBinaryTree(root: TreeNode) → int[]Examples
Example 1
root = [1,null,2,3,4]return = [1,3,4,2]Three and four are leaves, and two closes the right boundary.
Example 2
root = [1,2,3,4,5,6,7]return = [1,2,4,5,6,7,3]The boundary walks around the complete tree without duplicates.
Constraints
- The tree contains between
0and10^5nodes. -10^9 <= Node.val <= 10^9.