Please tell me what is wrong with this code

#include<iostream>
int main() {
	return 0;
}import java.util.*;

class Node{
    int data;
    Node left,right;
    Node(int data){
        this.data = data;
        left=right=null;
    }
}

class Main{
    static  Node root = null;
    static Node Insert(Node root,int data){
        if(root == null){
            root = new Node(data);
        }
        if(data <= root.data){
            root.left = Insert(root.left,data);
        }else{
            root.right = Insert(root.right,data);
        }

        }

    static void preorder(Node root){
        if(root == null)return;
        System.out.print(root.data + " ");
        preorder(root.left);
        preorder(root.right);
    }
    static void pre(Node node, int lo, int hi) {

        if (node == null) {
            return;
        }

        if (node.data < lo) {
            pre(node.right, lo, hi);
        } else if (node.data > hi) {
            pre(node.left, lo, hi);
        } else {

            pre(node.left, lo, hi);
            System.out.println(node.data);
            pre(node.right, lo, hi);
        }

    }

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int t = scan.nextInt();
        while(t-- >0){
            int n =scan.nextInt();
            int[] arr =  new int[n];
            for(int i=0;i<n;i++)Insert(scan.nextInt());
            int l = scan.nextInt();
            int r = scan.nextInt();
            System.out.print("# Preorder : ");
            preorder(root);
            System.out.println();
            System.out.print("# Nodes within range are : ");
            pre(root,l,r);

        }
    }
}

import java.util.*; class Node{ int data; Node left,right; Node(int data){ this.data = data; left=right=null; } } class Main{ static Node root = null; static void Insertt(int data){ root = Insert(root,data); } static Node Insert(Node root,int data){ if(root == null){ return new Node(data); } if(data <= root.data){ root.left = Insert(root.left,data); }else{ root.right = Insert(root.right,data); } return root; } static void preorder(Node root){ if(root == null)return; System.out.print(root.data + " "); preorder(root.left); preorder(root.right); } static void pre(Node root, int l, int h) { if (root == null) { return; } if (root.data < l) { pre(root.right, l, h); } else if (root.data > h) { pre(root.left, l, h); } else { pre(root.left, l, h); System.out.print(root.data + " “); pre(root.right, l, h); } } public static void main(String[] args){ Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-- >0){ int n =scan.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++)Insertt(scan.nextInt()); int l = scan.nextInt(); int r = scan.nextInt(); System.out.print(”# Preorder : “); preorder(root); System.out.println(); System.out.print(”# Nodes within range are : "); pre(root,l,r); } } }

Hey @virutyagi9
The simple approach is just to iterate in the tree (inorder let’s say). If the value of current node is in between the range k1 and k2(assuming k1<=k2, also if k2>k1: swap(k1,k2)) then recursively call left and right.
If the value of current node is smaller than k1, then just call the right function(why? , because since current is smaller than k1 then all the nodes left to it will also be smaller than k1) and if the value of current node is more than k2, then just simply call the function for curr->left(again similar logic used as before).
To debug just print which function are you calling at each point. Just give it one more shot. If you’re still not able to solve, I’ll share my code.

Bro … approach toh Shi lgai thi and tree bhi thik he bnaya … output thik aara h but hidden test case wrong aara h.

Check for edge cases like if the root is NULL in the first place only.
https://ide.codingblocks.com/s/232053.
This approach worked for me, and try printing stuff at each step for better understanding.

brother check this https://ide.codingblocks.com/s/232074