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;
}
};

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

  5. 【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 ...

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

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

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

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

  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. js 对url进行编码和解码的三种方式

    一.escape 和 unescape escape 原理:对除 ASCII字母.数字.标点符号(@ * _ + - . /) 以外的字符进行编码 .编码的字符被替换成了十六进制的转义序列 不编码的字 ...

  2. 【Linux】CentOS7 安装rabbitmq

    [1.安装erlang环境]yum install http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64. ...

  3. When should you use a class vs a struct in C++?

    Question: In what scenarios is it better to use a struct vs a class in C++? Answer: The only differe ...

  4. Why is one loop so much slower than two loops?

    Question: Suppose a1, b1, c1, and d1 point to heap memory and my numerical code has the following co ...

  5. Python全栈学习_day010作业

    1,继续整理函数相关知识点,写博客. 2,写函数,接收n个数字,求这些参数数字的和.(动态传参)def MySum(*args): sum = 0 for i in range(len(args)): ...

  6. html5新特性学习笔记

    1.语义化标签兼容问题(语义化标签只支持ie8以上,不包括ie8) 解决方法一:该标签的css中加上display:block; 通过DOM的方式创建这个标签 document.createEleme ...

  7. MySQL chartset

    -- # https://dev.mysql.com/doc/refman/8.0/en/charset-database.html -- create database aixinyz; -- 默認 ...

  8. 2017-10-10 都市传说: "部分"中文出现乱码

    知乎原链, 作者亦本人 事情起源于项目另一开发者在中文Windows下构建时遇到的部分中文出现乱码问题. 当时很不解的是, 为什么会只有部分出现乱码. 第一感觉是, 如果编码转换不正确, 要么全乱码, ...

  9. loadrunner 运行脚本-Run-time Settings之Pacing设置

    运行脚本-Run-time Settings之Pacing设置 by:授客 QQ:1033553122   As soon as the previous iteration ends 前一个迭代一结 ...

  10. [20170825]11G备库启用DRCP连接3.txt

    [20170825]11G备库启用DRCP连接3.txt --//昨天测试了11G备库启用DRCP连接,要设置alter system set audit_trail=none scope=spfil ...