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

Approach #1: C++.[rescursive]

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (root == nullptr) root = new TreeNode(val);
if (root->val > val) root->left = insertIntoBST(root->left, val);
if (root->val < val) root->right = insertIntoBST(root->right, val);
return root;
}
};

  

Approach #2: Java.[iterator]

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) root = new TreeNode(val);
TreeNode cur = root;
while (true) {
if (cur.val <= val) {
if (cur.right != null) cur = cur.right;
else {
cur.right = new TreeNode(val);
break;
}
} else {
if (cur.left != null) cur = cur.left;
else {
cur.left = new TreeNode(val);
break;
}
}
}
return root;
}
}

  

Approach #3: Python.[recursive]

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def insertIntoBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
if not root:
return TreeNode(val);
if root.val < val:
root.right = self.insertIntoBST(root.right, val)
elif root.val > val:
root.left = self.insertIntoBST(root.left, val) return root

  

Insert into a Binary Search Tree的更多相关文章

  1. [Swift]LeetCode701. 二叉搜索树中的插入操作 | 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 t ...

  2. [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 t ...

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

  4. 【leetcode】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, in ...

  5. 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. LeetCode题解之Insert into a Binary Search Tree

    1.题目描述 2.分析 插入算法. 3.代码 TreeNode* insertIntoBST(TreeNode* root, int val) { insert(root, val); return ...

  7. leetcode701. Insert into a Binary Search Tree

    https://www.cnblogs.com/grandyang/p/9914546.html 类似于二分查找的方法,用迭代的方法去做 注意:无论是进入左子树还是右子树,左右子树都变成了新的数,所以 ...

  8. [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  9. 算法与数据结构基础 - 二叉查找树(Binary Search Tree)

    二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...

随机推荐

  1. Vector 源码阅读

    Vector在功能上与ArrayList是类似的,实现的数据结构也是一样的.但Vector是线程安全的,ArrayList是线程不安全的.

  2. SHA-1算法c语言实现

    安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准 (Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signatu ...

  3. STO存在哪些潜在隐患?

    STO(Security Token Offering),即证券型通证发行,无疑是现目前区块链圈子讨论最热门的话题之一,纵使STO有很好的前景,但是其潜在隐患也不得不引起重视. 第一,STO与分布式网 ...

  4. servlet 复习笔记

    总的说来Servlet的配置包括Servlet的名字,Servlet的类(如果是JSP,就指定JSP文件),初始化参数,启动装入的优先级,servlet的映射,运行的安全设置. 下面举例介绍其配置: ...

  5. Machine Learning No.10: Anomaly detection

    1. Algorithm 2. evaluating an anomaly detection system 3. anomaly detection vs supervised learning 4 ...

  6. zabbix监控系统性能采集指标

                  监控项目                        详细内容                                                       ...

  7. mooc_java 集合框架上 学生所选课程

    用一个集合Set存放学生所选课程接口不能实例化,所以用其实现类进行实例化 set接口不能实例化,通过HashSet这个实现类来进行实例化,需要导入包this.courses=new HashSet() ...

  8. c# 实现WebSocket

    用C# ASP.NET MVC 实现WebSocket ,对于WebSocket想必都很了解了,不多说. 东西做的很粗糙 只能实现基本的聊天功能,不过基本的通信实现了,那么后序的扩展应该也不难(个人这 ...

  9. 64位 Windows 用了 32位编译平台 编译不过 MySQL API

    发生在一周前的事情了,当时想感受下 MySQL C API ,就写了几个小例子.虽然是在 Windows(我的工作电脑是 64位 Windows) 上面,但是不想用 VS ,只想用文本软件写好代码后用 ...

  10. 剑指offer12 打印从1到N位的所有数字,处理大整数情况

    /** * */ package jianzhioffer; /** * @Description 输入n位数,输出0-N的所有数 * @author liutao * @data 2016年4月22 ...