-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode第701题.cpp
More file actions
36 lines (36 loc) · 832 Bytes
/
Leetcode第701题.cpp
File metadata and controls
36 lines (36 loc) · 832 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
class Solution {
private:
void Traverse(TreeNode* &root, int val)
{
if(!root){
return;
}
if(!root->left&&val<root->val){
TreeNode *temp=new TreeNode(val);
root->left=temp;
return;
}
if(!root->right&&val>root->val){
TreeNode *temp=new TreeNode(val);
root->right=temp;
return;
}
else{
if(val<root->val){
Traverse(root->left, val);
}
else{
Traverse(root->right, val);
}
}
}
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if(!root){
TreeNode *temp=new TreeNode(val);
return temp;
}
Traverse(root,val);
return root;
}
};