[LeetCode] 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.html

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.java

For example, node

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:post

         5
       /   \
      2     7
     / \   
    1   3
         \
          4

 

这道题让咱们在二叉搜索树中插入结点,当前还须要保持二叉搜索树的性质,那么插入结点的方式就有多种,就像题目中给的那个例子。结点5能够有不一样的方法,可是很显然,放在结点7的左子结点比代替结点4成为根结点要来的简单许多。怎么简单咱们就怎么来,因此仍是按照简单的来吧。因为二叉搜索树自带二分的性质,那么首先根结点比较,若是大于根结点值的话,说明确定要插入到右子树中。因此接下来跟7比较,对于递归函数来讲,结点7也能够看成是一个新的根结点,那么因为结点7的值大于目标值5,因此要去其左子树,咱们发现其左子结点为空,那么咱们就能够根据目标值来生成一个新的结点,而后连到结点7的左子树上便可。那么在递归函数中,首先判断当前结点是否为空,为空的话就新建一个结点返回。不然就判断当前结点值是否大于目标值,是的话就对左子结点调用递归函数,并将返回值赋给当前结点的左子结点,不然就对右子结点调用递归函数,并将返回值赋给当前结点的右子结点,最后返回当前结点便可,参见代码以下:this

 

解法一:url

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

 

咱们也能够不使用递归来作,而是用迭代。总体思路跟递归并无太大的区别,但没有递归写法简洁。首先仍是判空,若为空,就新建结点返回。而后用一个变量cur来遍历,在while循环中,若是当前值大于目标值,若是其左子结点不存在,那么咱们新建结点,并连上其左子结点,并跳出循环;若左子结点存在,则cur指向其左子结点。不然,当前值小于目标值,若其右子结点不存在,新建结点并连上其右子结点,并跳出循环;若右子结点存在,则cur指向其右子结点。最后返回root便可,参见代码以下:spa

 

解法二:code

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if (!root) return new TreeNode(val);
        TreeNode *cur = root;
        while (true) {
            if (cur->val > val) {
                if (!cur->left) {cur->left = new TreeNode(val); break;}
                cur = cur->left;
            } else {
                if (!cur->right) {cur->right = new TreeNode(val); break;}
                cur = cur->right;
            }
        }
        return root;
    }
};

 

相似题目:htm

Search in a Binary Search Tree

 

参考资料:

https://leetcode.com/problems/insert-into-a-binary-search-tree/

https://leetcode.com/problems/insert-into-a-binary-search-tree/discuss/150757/java-iterative-100

https://leetcode.com/problems/insert-into-a-binary-search-tree/discuss/164137/Java-easy-to-understand-solution

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索