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 ...
随机推荐
- Vue框架创建项目常遇到问题
利用npm安装cnpm时出现的错误 npm WARN deprecated socks@1.1.10: If using 2.x branch, please upgrade to at least ...
- 【actitivi】配置运行上遇到的问题
基础: 需要 问题1:对于activiti-admin,添加mysql-connector-java-5.1.47.jar后: Sun Apr 28 16:09:00 CST 2019 WARN: E ...
- linux c tcp p2p
江湖上一直都有这位哥哥的传说,也有很多人说自己就他的真身! 但是... 今天分享一下TCP连接的P2P demo,江湖的规矩也要与时俱进... ———————————————————————————— ...
- JavaIO流——简单对文件的写入及读取(三)
已经讲了写入和读取了,那么想要把一个文件的内容复制到另一个文件呢 不说太多,直接见代码 public static void copyFile(String srcFilename, String d ...
- 怎么让微信下载APK文件包,微信内置浏览器无法打开APP下载链接的解决方案
** 做微信营销活动或者APK下载推广时候,域名被经常被封,做到微信中正常使用呢?这就要借助一些工具来实现有效的操作.** 先来认识一下微信屏蔽的原理.按原理逐个攻破,本人做防封一年来自认为得心应手, ...
- caffe程序中出现的db.cpp:#line(行号) unknown database backend问题
报错原因:lmdb不可用 解决方法:Makefile.config将此处更改 CPU_ONLY := 1 #如果只使用CPU的话就改这个,使用GPU的不需要改 USE_OPENCV := 1 #有安装 ...
- 使用 dom4j 处理 xml (1)
解决问题需要,自己简单学习了一下dom4j 的基本用法: (1)读取 xml 文件: (2)修改 xml 文件. 需要的 jar 包: dom4j-xxx.jar (可以在 https://dom4j ...
- http协议中的响应代码从 1xx ~ 5xx,一共有41种
http协议中的响应代码从 1xx ~ 5xx,一共有41种 http://how2j.cn/k/http/http-response-code/572.html
- 纪念一下学写pipeline时脑子里的坑
用的是filespipeline,用的存储地址是images的地址 测试煎蛋ooxx首页,shell测试的时候返回很多列表,但是实际爬的时候一直只返回一条,很烦,一直测一直测,就是不行,后来才发现,首 ...
- selenium webdriver报错 ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
昨天跑的好好的代码,今天突然报错: ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接. 调查一下,原来是Chrome自动升级,而chrom ...