-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTRange.java
More file actions
49 lines (42 loc) · 944 Bytes
/
Copy pathBSTRange.java
File metadata and controls
49 lines (42 loc) · 944 Bytes
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
package bst;
import java.util.*;
class Node{
int data;
Node left;
Node right;
Node(int x){
data=x;
left=null;
right=null;
}
}
public class BSTRange {
Node root;
public void printNearNodes(Node root, int k1, int k2)
{
if(root==null)return;
Node curr=root;
if(k1<curr.data){
curr=curr.left;
printNearNodes(curr,k1,k2);
}
if((k1<=curr.data)&&(k2>=curr.data)){
System.out.println(curr.data + " ");
}
if(k2>curr.data){
curr=curr.right;
printNearNodes(curr,k1,k2);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BSTRange bst = new BSTRange();
bst.root = new Node(40);
bst.root.left = new Node(10);
bst.root.left.left = new Node(5);
bst.root.left.left.left = new Node(1);
bst.root.right = new Node(50);
bst.root.right.right = new Node(100);
bst.printNearNodes(bst.root, 5, 45);
}
}