forked from imnishant/GeeksforGeeks-Java-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreorderToPostooder.java
54 lines (52 loc) · 1.22 KB
/
preorderToPostooder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
import java.lang.*;
import java.io.*;
class Node
{
int data;
Node left;
Node right;
Node(int d)
{
data = d;
}
}
class Index
{
int index;
Index(int i)
{
index = i;
}
}
class GFG
{
static int index = 0;
static void postorder(int arr[], int min, int max, Index index)
{
if(index.index == arr.length)
return;
if(arr[index.index] < min || arr[index.index] > max)
return;
int val = arr[index.index];
index.index++;
postorder(arr, min, val, index);
postorder(arr, val, max, index);
System.out.print(val + " ");
}
public static void main (String[] args) throws Exception
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bf.readLine());
while(t-- > 0)
{
int n = Integer.parseInt(bf.readLine());
String s[] = bf.readLine().trim().split(" ");
int arr[] = new int[n];
for(int i=0 ; i<n ; i++)
arr[i] = Integer.parseInt(s[i]);
postorder(arr, Integer.MIN_VALUE, Integer.MAX_VALUE, new Index(0));
System.out.println();
}
}
}