Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Postorder Traversal

Easy
0/40
Average time to solve is 15m
profile
Contributed by
42 upvotes
Asked in companies
AmazonMicrosoftPaypal

Problem statement

You have been given a Binary Tree of 'n' nodes, where the nodes have integer values. Your task is to return its postorder traversal.


For Example:
For the given tree below,
Postorder traversal for the given tree will be [4, 5, 2, 3, 1]. Hence, the answer is [4, 5, 2, 3, 1].

Example

Detailed explanation ( Input/output format, Notes, Images )
Sample Input 1:
1 2 3 4 -1 -1 5 6 7 -1 -1 -1 -1 -1 -1
Sample Output 1:
6 7 4 2 5 3 1
Explanation:
The given tree is:

binaryTree

Postorder traversal for the given tree will be [6, 7, 4, 2, 5, 3, 1]. Hence, the answer is [6, 7, 4, 2, 5, 3, 1].
Sample Input 2:
10 20 11 -1 -1 -1 -1
Sample Output 2:
20 11 10
Expected time complexity:
The expected time complexity is O(n).
Constraints :
1 <= 'n' <= 10^5
0 <= 'data' <= 10^5     

where 'n' is the number of nodes and 'data' denotes the node value of the binary tree nodes.

Time limit: 1 sec
Full screen
Console