701. 二叉搜索树中的插入 *** 作——记录(C++)

701. 二叉搜索树中的插入 *** 作——记录(C++),第1张

701. 二叉搜索树中的插入 *** 作——记录(C++)
class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        TreeNode* m=root;
        TreeNode* x=root;
        while(root)
        {
            x=root;
            if(valval)
            {
                    root=root->left; 
            }
            else
            { 
                    root=root->right; 
            }
        }
        
        TreeNode* a=new(TreeNode);
        a->val=val;
        if(!m)
{
    return a;
}
         if(valval)
            {       
               
                   x->left=a; 
            }
            else
            { 
                    x->right=a; 
            }
           
         return m;
    }
   

};

做得有点慢,效果一般。

一直在想这么插入一个新点。

加油! 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5098027.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存