class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
TreeNode* m=root;
TreeNode* x=root;
while(root)
{
x=root;
if(val<root->val)
{
root=root->left;
}
else
{
root=root->right;
}
}
TreeNode* a=new(TreeNode);
a->val=val;
if(!m)
{
return a;
}
if(val<x->val)
{
x->left=a;
}
else
{
x->right=a;
}
return m;
}
};
做得有点慢,效果一般。
一直在想这么插入一个新点。
加油!