Write Out Every Route From the Root to a Leaf
Implement rootToLeafRoutes
You are given the root of a binary tree. Return every route that starts at the root and ends at a leaf (a node with no children), each written as the node values joined by "->". Routes that go through a node's left child must be listed before routes that go through its right child.
You can pass a growing string down the recursion, or you can keep one shared path that grows on the way down and shrinks (backtracks) on the way back up.
Example 1:
Input: root = [8,3,10,1,6,null,14,null,null,4]
Output: ["8->3->1","8->3->6->4","8->10->14"]
Example 2:
Input: root = [7]
Output: ["7"]
Example 3:
Input: root = []
Output: []
+ 11 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 - ●
Return every root-to-leaf route as a string of the node values joined by "->" (for example "8->3->1"). List the routes in left-to-right order of their leaves — routes through the left subtree before routes through the right subtree. An empty tree has no routes
root =
[8, 3, 10, 1, 6, null, 14, null, null, 4]