Problem
Given a binary tree, return the preorder traversal of its nodes’ values.
Example
1 | Input: [1,null,2,3] |
Boundary Conditions
- Will input list node be null?
Solution1
Method: Depth-first Search (Recursive)
Time Complexity: O(n)
Space Complexity: O(n)
1 | # Definition for a binary tree node. |
or
Solution2
Method: Depth-first Search (Iterative)
Time Complexity: O(n)
Space Complexity: O(n)
1 | # Definition for a binary tree node. |