|
U
Pro145. Compare the Time and Auxiliary Space
Easy
Problem Description
Compare the time and auxiliary space complexity of iterative and recursive tree traversal.
Input Format
- •Line 1: An integer N (0≤N≤105), the number of nodes.
- •Line 2: N space-separated integers representing the node values in level-order. (Omitted if N=0).
Output Format
Print the computed integer scalar or space-separated traversal sequence.
Example 1:
Input:
5 1 2 3 4 5
Output:
4 2 5 1 3
Explanation: Processing tree nodes in standard order.
Example 2:
Input:
1 42
Output:
42
Explanation: Single root node evaluation.
Example 3:
Input:
0
Output:
0
Explanation: Empty tree evaluation returns 0.
Constraints:
0 <= N <= 10^5-10^9 <= node.val <= 10^9Time complexity target: O(N)Auxiliary space target: O(H) where H is tree height
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online
Code
|Loading Code Editor...
SavedLn 1, Col 1
param_1 =
param_2 =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit