https://www.cnblogs.com/grandyang/p/9914546.html 

类似于二分查找的方法,用迭代的方法去做

注意:无论是进入左子树还是右子树,左右子树都变成了新的数,所以需要重新根据root->left = ....来重新生成

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root == NULL)
            return new TreeNode(val);
        if(root->val < val)
            root->right = insertIntoBST(root->right,val);
        if(root->val > val)
            root->left = insertIntoBST(root->left,val);
        return root;
    }
};

 

相关文章:

  • 2021-10-07
  • 2022-01-13
  • 2021-05-19
  • 2021-12-15
  • 2021-08-10
  • 2021-07-20
  • 2021-09-16
猜你喜欢
  • 2021-11-28
  • 2022-01-27
  • 2021-05-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
相关资源
相似解决方案