-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrees_Insertion.java
More file actions
89 lines (58 loc) · 1.49 KB
/
Copy pathTrees_Insertion.java
File metadata and controls
89 lines (58 loc) · 1.49 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
/*You are given a pointer to the root of a binary search tree and a value to be inserted into the tree. Insert this value into its appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function.
Input Format
You are given a function,
node * insert (node * root ,int value) {
}
node is defined as :
struct node
{
int data;
node * left;
node * right;
}node;
Constraints
No. of nodes in the tree 500
Output Format
Return the root of the binary search tree after inserting the value into the tree.
Sample Input
4
/ \
2 7
/ \
1 3
The value to be inserted is 6.
Sample Output
4
/ \
2 7
/ \ /
1 3 6
*/
/* Node is defined as :
class Node
int data;
Node left;
Node right;
*/
static Node Insert(Node root,int value) {
//LOGIC
//RECURSIVE APPROACH,
//IF THE ROOT IS NULL, CREATE A NEW NODE, EQUATE IT TO THE ROOT
//HOWEVER, IF VALUE IS LESS THAN ROOT.DATA, INSERT IN THE LEFT SUBTREE
//IF THE VALUE IS GREATER THAN ROOT.DATA, INSERT IN THE RIGHT SUBTREE
//
if(root == null) {
Node newNode = new Node();
newNode.left = null;
newNode.right = null;
newNode.data = value;
root = newNode;
}
if(value > root.data) {
root.right = Insert(root.right, value);
}
if(value < root.data) {
root.left = Insert(root.left, value);
}
return root;
}