leetcode701. Insert into a Binary Search Tree
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 【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 ...
- 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode题解之Insert into a Binary Search Tree
1.题目描述 2.分析 插入算法. 3.代码 TreeNode* insertIntoBST(TreeNode* root, int val) { insert(root, val); return ...
- [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 ...
- 算法与数据结构基础 - 二叉查找树(Binary Search Tree)
二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...
随机推荐
- c#实战开发:以太坊Geth 常用命令 (四)
首先运行客户端 当前命令分为 eth,web3 ,personal ,net 输入 >eth 可以看到该命令下的所有方法 > eth 1.创建用户 personal.newAccount ...
- 【Java深入研究】6、CGLib动态代理机制详解
一.首先说一下JDK中的动态代理: JDK中的动态代理是通过反射类Proxy以及InvocationHandler回调接口实现的 但是,JDK中所要进行动态代理的类必须要实现一个接口,也就是说只能对该 ...
- Apollo源码阅读笔记(一)
Apollo源码阅读笔记(一) 先来一张官方客户端设计图,方便我们了解客户端的整体思路. 我们在使用Apollo的时候,需要标记@EnableApolloConfig来告诉程序开启apollo配置,所 ...
- echarts2.0仪表盘
option = { backgroundColor: '#0e0b2a', tooltip : { formatter: "{a} <br/>{b} : {c}%" ...
- 初学HTML-9
详情和概要标签:利用summary标签来描述概要信息,利用details标签来描述详情信息. 默认情况下是折叠显示. 格式:<details> <summary>概要信息< ...
- 初学HTML-5
表格标签:用来给一堆数据添加表格语义. 格式:<table> <tr> <td></td> </tr> </table> tab ...
- canvas-star0.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Html/Css 初步认识笔记
1.什么是 HTML ? HTML(HyperText Markup Language) 的学名是超文本标记语言. 标记用来表示网页内容要如何显示,自身不显示 .<我就是标记> 标记成对出 ...
- Linux 下的 PostgreSQL 数据库+文件通用自动备份脚本
由于 Odoo(原名 OpenERP)自 8.0 以来会生成 CSS,并把附件存储在文件系统中,因此以前单纯备份数据库的脚本已经不够用了.出于实际部署的考虑,我专门写了个较为通用的 Odoo 备份脚本 ...
- api接口签名认证的一种方式
请求方 try { using (var client = new HttpClient()) { StringContent content = new StringContent(strParam ...