-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.java
More file actions
105 lines (92 loc) · 2.71 KB
/
Copy pathbinaryTree.java
File metadata and controls
105 lines (92 loc) · 2.71 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.io.*;
class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;
public BinaryTreeNode(T data) {
this.data = data;
}
}
public class binaryTree {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));;
static StringTokenizer st;
public static BinaryTreeNode<Integer> takeInput() throws IOException {
st = new StringTokenizer(br.readLine());
int rootData = Integer.parseInt(st.nextToken());
if (rootData == -1) {
return null;
}
Queue<BinaryTreeNode<Integer>> pendingNodes = new LinkedList<>();
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(rootData);
pendingNodes.add(root);
while (!pendingNodes.isEmpty()) {
BinaryTreeNode<Integer> currentNode;
try {
currentNode = pendingNodes.remove();
} catch (Exception e) {
return null;
}
int leftChildData = Integer.parseInt(st.nextToken());
if (leftChildData != -1) {
BinaryTreeNode<Integer> leftChild = new BinaryTreeNode<Integer>(leftChildData);
currentNode.left = leftChild;
pendingNodes.add(leftChild);
}
int rightChildData = Integer.parseInt(st.nextToken());
if (rightChildData != -1) {
BinaryTreeNode<Integer> rightChild = new BinaryTreeNode<Integer>(rightChildData);
currentNode.right = rightChild;
pendingNodes.add(rightChild);
}
}
return root;
}
public static void main(String[] args) throws NumberFormatException, IOException {
try
{
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e)
{
System.err.println("Error");
}
BinaryTreeNode<Integer> root = takeInput();
int k = Integer.parseInt(br.readLine());
System.out.println(Solution.searchInBST(root,k));
}
}
class Solution {
/* Binary Tree Node class
*
* class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;
public BinaryTreeNode(T data) {
this.data = data;
}
}
*/
public static BinaryTreeNode<Integer> searchInBST(BinaryTreeNode<Integer> root , int k){
/* Your class should be named Solution
* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
if(root==null)
return null;
if(k==root.data)
return root;
else if(k>root.data)
return searchInBST(root.right,k);
else
return searchInBST(root.left,k);
}
}