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 ...
随机推荐
- Nginx反向代理部署NodeJS项目
在nginx配置文件种的http节点下: server { listen 8005; server_name localhost; location /{ proxy_set_header X_Rea ...
- Redis单机多节点集群实验
第一步:安装Redis 前面已经安装过了 不解释, Reids安装包里有个集群工具,要复制到/usr/local/bin里去 cp redis-3.2.9/src/redis-trib.rb /usr ...
- 使用vue+elementUI+springboot创建基础后台增删改查的管理页面--(1)
目前这家公司前端用的是vue框架,由于在之前的公司很少涉及到前端内容,对其的了解也只是会使用js和jquery,所以..慢慢来吧. 在此之前需要先了解vue的大致语法和规则,可先前往官方文档进行学习h ...
- FPGA小例子
AND ---与门:OR --- 或门:INV --- 非门:NAND --- 与非门:NOR --- 或非门:XOR --- 异或门:XNOR ---同或门:MUX --- 数据选择器: 1.使用一 ...
- div 拖拽
html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- java-concurrent包
通常所说的concurrent包基本有3个package组成 java.util.concurrent:提供大部分关于并发的接口和类,如BlockingQueue,Callable,Concurren ...
- Leetcode Articles: Insert into a Cyclic Sorted List
Given a node from a cyclic linked list which has been sorted, write a function to insert a value int ...
- JS对象、构造器函数和原型对象之间的关系
一.基本概念 1.对象:属性和方法的集合,即变量和函数的封装.每个对象都有一个__proto__属性,指向这个对象的构造函数的原型对象. 2.构造器函数:用于创建对象的函数,通过new关键字生成对象. ...
- mongodb非关系型数据库
mongodb非关系型数据库(对象型数据库): 优势:易扩展:灵活的数据模型:大数据量,高性能(读写) 关系型:(一对多.多对多.一对一)扩展性差,大数据下压力大,表结构更改困难(数据小时使用Mysq ...
- 使用vscode调试小段的typescript代码
最近在学习typescript.学习 嘛,当然免不了各种练习,试错.那么使用vscode就可以很方便的做到. 首先是安装node.js.我们知道,node.js提供了js脱离浏览器的执行平台.node ...