-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInBinarySearchTree.java
More file actions
90 lines (74 loc) · 2.68 KB
/
SearchInBinarySearchTree.java
File metadata and controls
90 lines (74 loc) · 2.68 KB
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
package Algorithms.BinaryTrees;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 29 April 2025
*/
public class SearchInBinarySearchTree {
static class TreeNode {int val; TreeNode left, right; TreeNode(int x){val = x; left = right = null;}}
public static void main(String[] args) {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.right = new TreeNode(7);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(9);
int val = 2;
System.out.println("searchBST(root, val) => " + searchBST(root, val));
}
// Binary Search O(log n)
public static TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) return root;
if (val < root.val) return searchBST(root.left, val);
return searchBST(root.right, val);
}
// Binary Search O(log n)
public TreeNode searchBSTMyApproach(TreeNode root, int val) {
if(root == null) return null;
else if (root.val == val) return root;
return root.val > val? searchBST(root.left, val) : searchBST(root.right, val);
}
// Binary Search O(log n)
public TreeNode searchBST2(TreeNode root, int val) {
while(root != null && root.val != val) {
root = root.val > val ? root.left : root.right;
}
return root;
}
// Binary Search O(log n)
public TreeNode searchBST3(TreeNode root, int val) {
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
while(!q.isEmpty()) {
TreeNode node = q.poll();
if(node == null) continue;
if(node.val == val) return node;
if(node.val > val) q.add(node.left);
else q.add(node.right);
}
return null;
}
/**
* same like python internal function, we can use internal class (with constructor) and don't need to pass the params
*/
public TreeNode searchBST4(TreeNode root, int val) {
final TreeNode[] result = new TreeNode[1]; // array of TreeNode of size 1
class Traverse{
Traverse(TreeNode currentNode){
if(currentNode.val == val){
result[0] = currentNode;
}
if (currentNode.left != null) {
new Traverse(currentNode.left);
}
if (currentNode.right != null) {
new Traverse(currentNode.right);
}
}
}
new Traverse(root);
return result[0];
}
}