-
Notifications
You must be signed in to change notification settings - Fork 5
/
BSTnodeRange.java
102 lines (93 loc) · 1.99 KB
/
BSTnodeRange.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
while(testcases-->0)
{
Node root=null;
int sizeOfArray=sc.nextInt();
int arr[]=new int[sizeOfArray];
for(int i=0;i<sizeOfArray;i++)
{
int x=sc.nextInt();
arr[i]=x;
}
for(int i=0;i<sizeOfArray;i++)
{
root=Geeks.newNode(root,arr[i]);
}
int l,h;
l=sc.nextInt();
h=sc.nextInt();
System.out.println(Geeks.getCountOfNode(root,l,h));
}
}
}
class Node
{
int data;
Node left;
Node right;
}
class Geeks
{
public static Node createNewNode(int value)
{
Node temp=new Node();
temp.data=value;
temp.left=null;
temp.right=null;
return temp;
}
static public Node newNode(Node root,int data)
{
if(root==null)
root=createNewNode(data);
else if(data<root.data)
root.left=newNode(root.left,data);
else
root.right=newNode(root.right,data);
return root;
}
//Position this line where user code will be pasted.
}
}
/*Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function is mentioned above.*/
// A Binary Search Tree node
/*
Structure of node
class Node
{
int data;
Node left;
Node right;
}
*/
//Complete this function
static int count;
public static int getCountOfNode(Node root,int l, int h)
{
//Your code here
count = 0;
return getCountUtil(root,l,h);
}
public static int getCountUtil(Node root, int l, int h)
{ if(root == null)
return 0;
if(root.data > h)
return getCountUtil(root.left,l,h);
if(root.data< l)
return getCountUtil(root.right,l,h);
else{
count++;
getCountUtil(root.right,l,h);
getCountUtil(root.left,l,h);
}
return count;
}