dsa · easy
Binary Tree Paths
Return every root-to-leaf path in root as a string. Join node values along a path with "->". A leaf has no children.
Arguments
root— binary tree as nested [val, left, right] or None
Each node is [val, left, right] or None. Order of the strings does not matter. An empty tree yields [].
Example
root = [1, [2, None, [5, None, None]], [3, None, None]].
One leaf is 5 via 1 → 2 → 5, written "1->2->5". The other leaf is 3 via 1 → 3, written "1->3". Return those two strings in any order.
Constraints
Number of nodes in [0, 100] -100 <= val <= 100 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [1, [2, None, [5, None, None]], [3, None, None]] Expected: ["1->2->5","1->3"]
Example 2
Input: [1, None, None] Expected: ["1"]