-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path701.cpp
More file actions
24 lines (24 loc) · 669 Bytes
/
Copy path701.cpp
File metadata and controls
24 lines (24 loc) · 669 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
class Solution {
public:
TreeNode *insertIntoBST(TreeNode *root, int val) {
TreeNode *child = root;
while (child) {
if (val > child->val) {
if (!child->right) {
auto *temp = new TreeNode(val);
child->right = temp;
break;
}
child = child->right;
} else {
if (!child->left) {
auto *temp = new TreeNode(val);
child->left = temp;
break;
}
child = child->left;
}
}
return root;
}
};