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

Postorder Traversal

Easy
0/40
profile
Contributed by
18 upvotes
Asked in companies
GoogleAmazon

Problem statement

You have been given a Binary Tree of 'N' nodes, where the nodes have integer values. Your task is to find the Post-Order traversal of the given binary tree.

For example :
For the given binary tree:

The Postorder traversal will be [5, 2, 3, 7, 6, 4, 1].
Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 10
0 <= N <= 3000
0 <= data <= 10^9     

Where 'data' denotes the node value of the binary tree nodes.

Time limit: 1 sec
Sample Input 1 :
2
1 2 3 -1 -1 -1  6 -1 -1
1 2 3 -1 -1 -1 -1
Sample Output 1 :
2 6 3 1
2 3 1
Explanation of Sample Output 1 :
 In test case 1, the given binary tree is shown below:

Postorder traversal of given tree = [2, 6, 3, 1]

In test case 2, the given binary tree is shown below:

Postorder traversal of given tree = [2, 3, 1]
Sample Input 2 :
2
1 -1 -1
1 2 4 5 3 -1 -1 -1 -1 -1 -1
Sample Output 2 :
1
5 3 2 4 1
Explanation of Sample Output 2 :
In test case 1, there is only one node, so Post-Order traversal will be only [1].

In test case 2, the given binary tree is shown below:

Postorder traversal of given tree = [5, 3, 2, 4, 1]
Full screen
Console