453. Pre-Order Walk
A company prints its org chart as a binary tree given by root: each manager has at most a left and a right direct report. To announce a reorganisation, a memo must name every manager before any of their reports, visiting the left team before the right team.
Return the node values in pre-order: a node's value first, then every value of its left subtree, then every value of its right subtree. The result is an array containing one entry per node, and an empty tree gives an empty array.
Example 1
- Input:
- root = [10,4,15,null,6]
- Output:
- [10,4,6,15]
- Explanation:
Visit 10, then its left subtree (4 and then its right child 6), then the right child 15.
Example 2
- Input:
- root = [2,null,7,null,9]
- Output:
- [2,7,9]
- Explanation:
Each node has only a right child, so the order follows the chain 2, 7, 9.
Constraints
0 ≤ number of nodes ≤ 105
-1000 ≤ node value ≤ 1000
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n)
- Space
- O(h)