forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
42 lines (35 loc) · 813 Bytes
/
Solution.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
/* Node is defined as :
class Node
int data;
Node left;
Node right;
*/
static Node Insert(Node root, int value) {
if (root == null) {
root = new Node();
root.data = value;
return root;
}
boolean inserted = false;
Node current = root;
Node newNode = new Node();
newNode.data = value;
while (!inserted) {
if (current.data < value) {
if (current.right == null) {
current.right = newNode;
inserted = true;
} else {
current = current.right;
}
} else {
if (current.left == null) {
current.left = newNode;
inserted = true;
} else {
current = current.left;
}
}
}
return root;
}