Instructions
You are given the root of a binary tree. Return the values on its boundary, going anticlockwise around the tree, starting at the root.
A leaf is a node with no children. In this problem the root is never a leaf, even when it has no children.
The answer is four parts, joined in this order:
- The root.
- The left boundary, from top to bottom. If the root has no left child, this part is empty. Otherwise start at the root's left child and repeat: if the current node is a leaf, stop (do not write it). If not, write its value, then move to its left child if it has one, or to its right child if it has no left child.
- All leaves, from left to right. These are the leaves in the order a pre-order walk (node, left, right) meets them.
- The right boundary, from bottom to top. This is the mirror of part 2: start at the root's right child (empty if there is none), write each node that is not a leaf, move to the right child if there is one, otherwise to the left child, and stop at a leaf. Then write these values in reverse order.
No node is written twice: leaves are only in part 3, and parts 2 and 4 are in different halves of the tree.
A small walk-through
Root: 1. Left boundary: start at 2 (not a leaf, write it); 2 has no left child, so move right to 4, which is a leaf, so stop. Leaves from left to right: 4, 7, 6 (5 is not a leaf, it has the child 7). Right boundary: start at 3 (write it), move right to 6, a leaf, so stop; reversed it is still 3. The answer is [1, 2, 4, 7, 6, 3].
How the input is shown
The tree is written in level order: the values row by row, from left to right, with null where a child is missing. The tree above is [1, 2, 3, null, 4, 5, 6, null, null, 7]. Your function gets the TreeNode root, with fields val, left and right, and returns an array of numbers.
Examples
Example 1
- Input:
- [10,6,15,null,8,12,20,7,9,null,null,18]
- Output:
- [10,6,8,7,9,12,18,20,15]
Root 10. Left boundary: 6, which has no left child, so the walk turns right to 8, then goes left to the leaf 7 and stops: [6, 8]. Leaves from left to right: 7, 9, 12, 18. Right boundary: 15, then right to 20, which has no right child, so the walk turns left to the leaf 18 and stops: [15, 20], written in reverse as 20, 15.
Example 2
- Input:
- [1,null,2,3,4]
- Output:
- [1,3,4,2]
The root has no left child, so the left boundary is empty. The leaves are 3 and 4. The right boundary is just 2 (its right child 4 is a leaf). So the answer is 1, then 3 and 4, then 2.
Example 3
- Input:
- [42]
- Output:
- [42]
A tree with one node. The root is written once, and it is not counted again as a leaf.
Constraints
- The tree has between
1and40nodes. 1 <= Node.val <= 99(values can repeat).
3 examples run when you press Run. Submit checks every hidden test too.
- Input:
- [10,6,15,null,8,12,20,7,9,null,null,18]
- Expected:
- [10,6,8,7,9,12,18,20,15]
- Input:
- [1,null,2,3,4]
- Expected:
- [1,3,4,2]
- Input:
- [42]
- Expected:
- [42]
Run checks your code against the 3 examples. Submit checks it against all 163 tests and needs a free account. You can also try ideas in the free JavaScript or Python compiler.