P14414 [GeeksforGeeks] Nodes at odd levels
普及-
题目描述

Given a binary tree of size N, find all the nodes at odd levels.Root is considered at level 1.
Example 1:
Input:
1
/ \
2 3
/ \ \
4 5 6
/ \ /
7 8 9
Output 1 4 5 6
Example 2:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7

Output: 1 4 5 6 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function nodesAtOddLevels() which takes root node of the tree as input parameter and returns an list of all the nodes at odd levels in preorder.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= Number of nodes <= 10^{3}
1 <= Data of a node <= 10^{3}

输入格式

见题面

输出格式

见题面

样例输入
1 / \ 2 3 / \ \ 4 5 6 / \ / 7 8 9
样例输出
1 4 5 6