LeetCode C++ 701. Insert into a Binary Search Tree【二叉搜索树】中等

Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.

Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.

For example, 

Given the tree:
        4
       / \
      2   7
     / \
    1   3
And the value to insert: 5

You can return this binary search tree:

         4
       /   \
      2     7
     / \   /
    1   3 5

This tree is also valid:

		 5	
       /   \
      2     7
     / \   
    1   3
         \
          4

Constraints:

  • The number of nodes in the given tree will be between 0 and 10^4.
  • Each node will have a unique integer value from 0 to -10^8, inclusive.
  • -10^8 <= val <= 10^8
  • It's guaranteed that val does not exist in the original BST.

题意:给定二叉搜索树的根节点和要插入树中的值,将值插入二叉搜索树,返回插入后二叉搜索树的根节点。 这里保证原始二叉搜索树中不存在新值。

题目提示我们,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。


思路1:递归插入

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

效率如下,感觉好低啊:

执行用时:176 ms, 在所有 C++ 提交中击败了5.65% 的用户
内存消耗:56 MB, 在所有 C++ 提交中击败了5.08% 的用户

思路2:迭代插入

先找到要插入的地方及其父结点,然后比较值,插入为左结点或右结点:

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if (root == nullptr) return new TreeNode(val);
        TreeNode *temp = root, *father = root;
        while (temp) {
            father = temp;
            temp = val < temp->val ? temp->left : temp->right;
        }
        if (val < father->val) father->left = new TreeNode(val);
        else father->right = new TreeNode(val);
        return root;
    }
};

效率也很低:

执行用时:168 ms, 在所有 C++ 提交中击败了5.65% 的用户
内存消耗:55.8 MB, 在所有 C++ 提交中击败了5.08% 的用户

用Java提交一下:

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) return new TreeNode(val);
        TreeNode temp = root, father = root;
        while (temp != null) {
            father = temp;
            temp = val < temp.val ? temp.left : temp.right;
        }
        if (val < father.val) father.left = new TreeNode(val);
        else father.right = new TreeNode(val);
        return root;
    }
}

这是赤裸裸的作弊吧???

执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:39.4 MB, 在所有 Java 提交中击败了56.71% 的用户

2020/9/30 Update:Python代码如下:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root: 
            return TreeNode(val)
        if val < root.val:
            root.left = self.insertIntoBST(root.left, val)
        else:
            root.right = self.insertIntoBST(root.right, val)
        return root
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

memcpy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值