P18930 [GeeksforGeeks] Preorder traversal
普及-
题目描述

Given a binary tree, find its preorder traversal.
Example 1:
Input:
1
/
4
/ \
4 2
Output: 1 4 4 2
Example 2:
Input:
6
/ \
3 2
\ /
1 2
Output: 6 3 1 2 2
Your Task:
You just have to complete the function preorder() which takes the root node of the tree as input and returns an array containing the preorder traversal of the tree.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= Number of nodes <= 10^{4}
0 <= Data of a node <= 10^{5}

输入格式

见题面

输出格式

见题面

样例输入
1 / 4 / \ 4 2
样例输出
1 4 4 2