Loading problem…
Given the root of a binary tree, return the preorder traversal of its nodes' values.
Example 1
Input: root = [1,null,2,3]
Output: [1,2,3]
Explanation: Preorder visits the node first, then its left subtree, then its right subtree. We visit 1, move to its right child 2, then to 2's left child 3, giving [1,2,3].
Example 2
Input: root = []
Output: []
Explanation: The tree is empty, so there is nothing to visit.
Example 3
Input: root = [1]
Output: [1]
Explanation: The only node is visited, giving [1].
Preorder visits the node first, then its left subtree, then its right subtree.
Run checks all cases above. Submit evaluates all test cases.