dsa · easy
Binary Tree Preorder Traversal
Return the **preorder** values of root: visit the node, then its left subtree, then its right subtree. Each node is [val, left, right] or None. An empty tree yields [].
Arguments
root— binary tree as nested [val, left, right] or None
Example
root = [1, None, [2, [3, None, None], None]].
Visit 1 first. Its left is empty, so walk the right child 2, then 2’s left child 3. The values are [1, 2, 3].
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, None, [2, [3, None, None], None]] Expected: [1,2,3]
Example 2
Input: None Expected: []