Lintcode85-Insert Node in a Binary Search Tree-Easy
85. Insert Node in a Binary Search Tree
Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree.
Example
Example 1:
Input: tree = {}, node = 1
Output: 1
Explanation:
Insert node 1 into the empty tree, so there is only one node on the tree.
Example 2:
Input: tree = {2,1,4,3}, node = 6
Output: {2,1,4,3,6}
Explanation:
Like this:
2 2
/ \ / \
1 4 --> 1 4
/ / \
3 3 6
Challenge
Can you do it without recursion?
思路:
在树上定位要插入节点的位置。
- 如果它大于当前根节点,则应该在右子树中,如果没有右子树则将该点作为右儿子插入;若存在右子树则在右子树中继续定位。
- 如果它小于当前根节点,则应该在左子树中,处理同上。
(二叉查找树中保证不插入已经存在的值)
二分法代码:
public TreeNode insertNode(TreeNode root, TreeNode node) {
if (root == null) {
return node;
}
if (root.val > node.val) {
root.left = insertNode(root.left, node);
} else {
root.right = insertNode(root.right, node);
}
return root;
}
Lintcode85-Insert Node in a Binary Search Tree-Easy的更多相关文章
- Lintcode: Insert Node in a Binary Search Tree
Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...
- 85. Insert Node in a Binary Search Tree【easy】
Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...
- LeetCode: 669 Trim a Binary Search Tree(easy)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
- 【leetcode】Convert Sorted Array to Binary Search Tree (easy)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序 ...
- [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 ...
随机推荐
- springboot新增swagger2配置
转自http://www.cnblogs.com/jtlgb/p/8532433.html SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人 ...
- 使用paginate分页后数据处理
public function index(){ $sql = ""; $list = ""; $pagenumber = 20;//默认分页条数 //查询数据 ...
- 【Idea】idea waiting until last debugger command completes
https://blog.csdn.net/KingBoyWorld/article/details/73440717 以上方法可以解决 另: 有说: 并未尝试 https://stackoverfl ...
- 做一个有产品思维的研发:Scrapy安装
每天10分钟,解决一个研发问题. 如果你想了解我在做什么,请看<做一个有产品思维的研发:课程大纲>传送门:https://www.cnblogs.com/hunttown/p/104909 ...
- oo第二次博客总结
不知道怎么说好呢,自己对自己也很没有信心的,希望自己能做出些许改变
- 干货 | LIDAR、ToF相机、双目相机如何科学选择?
点击"计算机视觉life"关注,置顶更快接收消息! 本文阅读时间约5分钟 本文翻译自卡内基梅隆大学 Chris asteroid 三维视觉技术的选择 传感器参数及定义 LIDAR ...
- 装PIL库
pip install Pillow -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
- 简介一下 i++和++i&&i=i+i,i+=1;的区别
首先: int i=2; i++=2; ++i=3; 前者先显示当前的值,而后者则是先自增在显示值: second i=i+1和i+=1; 输出结果虽然一样,但是 1.byte i=2; i+=2; ...
- Yarn 组件的指挥部 – 调度器Scheduler
linux基础 为hadoop集群的搭建扫清了障碍,也为内存的管理,文件系统的管理扫清了障碍 接着到Hadoop的阶段,首先做集群的安装,深入到使用这两个核心的组件,分布式文件系统HDFS,解决大量数 ...
- jq元素拖拽
<div id="a1"></div> js <script type="text/javascript"> $(funct ...