Given the root of a binary tree, return its preorder traversal as an array of node values. Preorder means you visit the current node first, then the left subtree, then the right subtree, so the order is fully deterministic for any tree shape.
If the tree is empty, return an empty array. The example below shows how the traversal proceeds, the constraints are small enough for either recursion or an explicit stack, and the follow-up asks you to think about the iterative version too.
Input / output
root: TreeNode (the JSON test fixture is a LeetCode-style level-order array, e.g. [1,null,2,3], using null for missing children)int[]The recursive solution is direct, but can you also solve it iteratively using an explicit stack?