-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1 . Searching.java
55 lines (44 loc) · 1.47 KB
/
1 . Searching.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
/*You are given a binary tree and a target value. Implement a BFS algorithm to find the target value in the binary tree and return true if the target exists in the tree, or false otherwise.*/
import java.util.LinkedList;
import java.util.Queue;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class BFSSearchBinaryTree {
public static boolean bfsSearch(TreeNode root, int target) {
if (root == null) {
return false;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode current = queue.poll();
if (current.val == target) {
return true;
}
if (current.left != null) {
queue.offer(current.left);
}
if (current.right != null) {
queue.offer(current.right);
}
}
return false;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(5);
root.left = new TreeNode(3);
root.right = new TreeNode(8);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(4);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(9);
int target = 4;
boolean result = bfsSearch(root, target);
System.out.println("Is " + target + " present in the binary tree? " + result);
}
}
``