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 ...
随机推荐
- tensorflow 基本内容
tensorflow的结构 1.使用图(graphs)来表示计算任务 2.在被称之为会话(Session)的上下文(context)中执行图 3.使用tensor表示数据 4.通过变量(Variabl ...
- Spring Batch(三) Job Launcher、ItemReader、ItemProcessor、ItemWriter各个实现类和用途
内容来自<Spring Batch 批处理框架>,作者:刘相. 1.JobLauncher JobLauncher(作业调度器)是SpringBatch框架提供的运行Job的能力.用过给定 ...
- extjs 跨域 ajax.request
https://www.cnblogs.com/yuzhongwusan/p/3677955.html https://stackoverflow.com/questions/25727306/req ...
- msbuild 编译指定工程时构建脚本的配置
有时候 ,我们编译windows的exe时,我们不需要编译所以的工程,我们只需要指定某个工程就好了,此时我们使用/t:工程名:Rebuild(如果要编译全部工程就把工程名去掉,即/t:Rebuild) ...
- numpy处理时间序列
1. 字符串转成numpy.datetime64格式 import numpy as np #将字符串转换成numpy格式时间 #注意个位前补0,如1月写成01 nd=np.datetime64('2 ...
- ubuntu 16.04 搭建tigervnc
主要参考 https://vitux.com/ubuntu-vnc-server/ apt install xfce4 xfce4-goodies -yapt install -y tightvncs ...
- iOS 高级去水印,涂鸦去水印
目前在研究一下图像的处理,看了一下相关的软件,比如:<去水印大师>,究竟去水印是怎么处理的呢?看图分析. 一共是三个功能:快速去水印.高级去水印.涂鸦去水印 快速去水印:暂时没找到好的处理 ...
- SQL死锁操作
这两天数据库经常被锁,所以记录一下操作: 查看被锁表:select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) ...
- P3974 [TJOI2015]组合数学
题目描述 为了提高智商,ZJY开始学习组合数学.某一天她解决了这样一个问题:给一个网格图,其中某些格子有财宝.每次从左上角出发,只能往右或下走.问至少要走几次才可能把财宝全捡完. 但是她还不知足,想到 ...
- Yii2.0 上使用 redis
1. 通过composer进行安装,到项目根目录cmd运行 php composer.phar require --prefer-dist yiisoft/yii2-redis或者添加 "y ...