Given the root of a binary tree, return the preorder traversal of its nodes' values.
Output: [1,2,3]
Output: [1,2,4,5,6,7,3,8,9]
Input: root = []
Output: []
Input: root = [1]
Output: [1]
[0, 100].-100 <= Node.val <= 100Follow up: Recursive solution is trivial, could you do it iteratively?
The recursive approach is the most straightforward way to perform a preorder traversal of a binary tree. In a preorder traversal, the root node is visited first, followed by the left subtree, and finally the right subtree. The recursive implementation mimics this order naturally by recursively calling the preorder function for each subtree.
An iterative approach using a stack is a common way to avoid recursion and control the function call behavior explicitly. The idea is to mimic the system's call stack with an explicit stack data structure to store nodes yet to be processed.
Morris Traversal modifies the tree structure temporarily to allow traversal of the tree without using additional space or a stack. It uses the concept of threading a binary tree.
current node to root.