-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfaraz.cpp
More file actions
43 lines (39 loc) · 965 Bytes
/
Copy pathfaraz.cpp
File metadata and controls
43 lines (39 loc) · 965 Bytes
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
#include<bits/stdc++.h>
using namespace std;
// Insert into a Binary Search Tree Problem
struct TreeNode{
TreeNode* left, *right;
int val;
TreeNode(int x){
val = x;
left = NULL;
right = NULL;
}
};
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if(root == NULL){
return new TreeNode(val);
}
TreeNode* curr = root;
while(true){
if(curr->val <= val){
if(curr->right != NULL) curr = curr->right;
else{
curr->right = new TreeNode(val);
// now break;
break;
}
}
else{
if(curr->left != NULL) curr = curr->left;
else{
curr->left = new TreeNode(val);
break;
}
}
}
return root;
}
};