Binary Tree Preorder Traversal
Tree
easy
Score: 20
Given the root
of a binary tree, return the preorder traversal of its nodes’ values.
Class TreeNode:
val (int)
left (TreeNode)
right (TreeNode)
Input Format
First Parameter - TreeNode root
Output Format
Return the array of values of nodes in preorder sequence.
Example 1:
Input:
1 null 3 null 5 7 6 null 8
Output:
1 3 5 7 8 6
Example 2:
Input:
15 10 12 13 null 5 null null 14
Output:
15 10 13 14 12 5
Constraints
- The number of nodes in the tree is in the range
[0, 100]
. - -100 <=
Node.val
<= 100 - Expected Time Complexity: O(n)
- Expected Space Complexity: O(1)