Walk Around the Outer Edge of the Tree
Implement outerEdge
You are given the root of a binary tree. List the nodes on its outer edge, going counter-clockwise, each node exactly once: first the root, then the left edge from top to bottom, then all the leaves from left to right, and finally the right edge from bottom to top. The left edge starts at the root's left child and always steps to the left child, or to the right child when there is no left child; the right edge is defined symmetrically. Leaves are never listed as part of an edge — they appear only in the leaves section.
The three parts can be produced by three separate walks, or by a single depth-first walk that remembers whether the current node lies on the left edge or the right edge.
Example 1:
Input: root = [20,8,22,4,12,null,25,null,null,10,14]
Output: [20,8,4,10,14,25,22]
Example 2:
Input: root = [5,null,9,7]
Output: [5,7,9]
Example 3:
Input: root = [6]
Output: [6]
+ 12 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes ≤ 100; −100 ≤ node.val ≤ 100 - ●
The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child - ●
Walk the outer edge counter-clockwise, each node once: (1) the root; (2) the LEFT EDGE from the root's left child downwards, always stepping to the left child when there is one, otherwise the right child, leaving out leaves; (3) ALL leaves from left to right; (4) the RIGHT EDGE, found the same way from the root's right child (right child preferred, otherwise left), leaving out leaves, listed from the bottom up - ●
A single-node tree returns just that node. An empty tree returns an empty list
root =