-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path701.cpp
More file actions
49 lines (42 loc) · 1.4 KB
/
701.cpp
File metadata and controls
49 lines (42 loc) · 1.4 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
// 701. Insert into a Binary Search Tree
// Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST.
// Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
//Compare the insert value with the current value, if larger, go to right branch,(if no right node, add it)
// if smaller, got to left branch(if no left node,add it).
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
TreeNode * curr = root;
while(true){
cout<<curr->val<<endl;
if (val > curr->val){
if(!curr->right){
curr->right = new TreeNode(val);
break;
}
else{
curr = curr->right;
}
}
if (val < curr->val){
if(!curr->left){
curr->left = new TreeNode(val);
break;
}
else{
curr = curr->left;
}
}
}
return root;
}
};