Skip to content

Commit a0f505a

Browse files
authored
Create Postorder Binary Tree
1 parent 798aa5f commit a0f505a

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
For a given Binary Tree of integers, print the post-order traversal.
3+
4+
Input Format:
5+
The first and the only line of input will contain the node data, all separated by a single space.
6+
Since -1 is used as an indication whether the left or right node data exist for root, it will not be a part of the node data.
7+
8+
Output Format:
9+
The only line of output prints the post-order traversal of the given binary tree.
10+
Constraints:
11+
1 <= N <= 10^6
12+
Where N is the total number of nodes in the binary tree.
13+
Time Limit: 1 sec
14+
15+
Sample Input 1:
16+
1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1
17+
Sample Output 1:
18+
4 5 2 6 7 3 1
19+
20+
Sample Input 2:
21+
5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1
22+
Sample Output 1:
23+
2 9 3 6 10 5
24+
*/
25+
26+
/*
27+
28+
Following the structure used for Binary Tree
29+
30+
class BinaryTreeNode<T> {
31+
T data;
32+
BinaryTreeNode<T> left;
33+
BinaryTreeNode<T> right;
34+
35+
public BinaryTreeNode(T data) {
36+
this.data = data;
37+
this.left = null;
38+
this.right = null;
39+
}
40+
}
41+
42+
*/
43+
44+
public class Solution {
45+
46+
public static void postOrder(BinaryTreeNode<Integer> root) {
47+
//Your code goes here
48+
if (root==null)
49+
{
50+
return;
51+
}
52+
postOrder(root.left);
53+
postOrder(root.right);
54+
System.out.print(root.data+" ");
55+
}
56+
57+
}

0 commit comments

Comments
 (0)