import java.util.*;
public class Main {
public int pIndex = 0;
public Node constructTree(int[] preorder, int data, int min, int max) {
if (pIndex < preorder.length) {
if (preorder[pIndex] > min && preorder[pIndex] < max) {
Node root = new Node(data);
pIndex++;
if (pIndex < preorder.length) {
root.left = constructTree(preorder, preorder[pIndex], min,
data);
root.right = constructTree(preorder, preorder[pIndex],
data, max);
}
return root;
}
}
return null;
}
public void displayTree(Node root) {
if (root != null) {
System.out.print(root.data+" “);
displayTree(root.left);
//System.out.print(” " + root.data);
displayTree(root.right);
}
}
public static void rangekey(Node root,int no1,int no2) {
if(root.left==null || root.right==null ) {
return ;
}
Stack stack =new Stack<>();
if(root.data>no1 && root.data<no2) {
stack.push(root.data);
}
rangekey(root.left, no1, no2);
rangekey(root.right, no1, no2);
while(!stack.isEmpty()) {
System.out.print(stack.pop() + " “);
}
}
public static void main(String args[]) {
Main p = new Main();
Scanner s=new Scanner(System.in);
int n =s.nextInt();
for(int i=0;i<n;i++){
int m =s.nextInt();
int[] preOrder = new int [m] ;
for(int j=0;j<preOrder.length;j++){
preOrder[j]=s.nextInt();
}
Node root = p.constructTree(preOrder, preOrder[0], Integer.MIN_VALUE,
Integer.MAX_VALUE);
System.out.print(”# Preorder : “);
p.displayTree(root);
System.out.println();
int m1=s.nextInt();
int m2 =s.nextInt();
System.out.print(”# Nodes within range are : “);
System.out.print(m1 +” ");
p.rangekey(root, m1, m2);
System.out.print(m2+ " ");
}
}
}
class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
}
}
Isme rune time error aa raha hai
hey @pandit.nipun007
try for this input
1
10
21 18 2 6 14 24 12 13 3 4
5 10
correct output : # Preorder : 21 18 2 6 3 4 14 12 13 24
Nodes within range are : 6
your code gives: # Preorder : 21 18 2 6 14 24
Nodes within range are : 5 10
sir can you correct the code