lintcode:在二叉查找树中插入节点
题目:
给定一棵二叉查找树和一个新的树节点,将节点插入到树中。
你需要保证该树仍然是一棵二叉查找树。
样例
给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的:



挑战
能否不使用递归?
解题:
递归的方法比较简单
Java程序:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of the binary search tree.
* @param node: insert this node into the binary search tree
* @return: The root of the new binary search tree.
*/
public TreeNode insertNode(TreeNode root, TreeNode node) {
// write your code here
if(root==null)
return node;
if(root.val>=node.val)
root.left = insertNode(root.left,node);
if(root.val<node.val)
root.right = insertNode(root.right,node);
return root;
}
}
总耗时: 1636 ms
非递归的感觉比较复杂。。。。
一直非递归的方法,就是一直走,一直走,走到没有路的时候就是插入的节点,这里是因为插入的节点一定是在新建的叶子节点,原理是二叉查找树是;1,根节点左子树的值比根节点小,右子树的值都比根节点大,2.左右子树也满足1的条件
下面程序定义的slow指针是用来做最后插入节点的父节点的
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of the binary search tree.
* @param node: insert this node into the binary search tree
* @return: The root of the new binary search tree.
*/
public TreeNode insertNode(TreeNode root, TreeNode node) {
// write your code here
if(root==null){
root=node;
return root;
}
TreeNode fast = root;
TreeNode slow = null;
while(fast!=null){// fast == null 的时候 slow就是其父结点,而这个空的就是要插入的结点位置
slow = fast;
if(fast.val>node.val){
fast = fast.left;
}else{
fast = fast.right;
}
}
if(slow!=null){
if(slow.val>node.val){
slow.left = node;
}else{
slow.right = node;
}
}
return root;
}
}
总耗时: 1753 ms
Python程序:
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: The root of the binary search tree.
@param node: insert this node into the binary search tree.
@return: The root of the new binary search tree.
"""
def insertNode(self, root, node):
# write your code here
if root==None:
return node
if root.val>=node.val:
root.left = self.insertNode(root.left,node)
if root.val<node.val:
root.right = self.insertNode(root.right,node)
return root
总耗时: 272 ms
非递归程序:
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: The root of the binary search tree.
@param node: insert this node into the binary search tree.
@return: The root of the new binary search tree.
"""
def insertNode(self, root, node):
# write your code here
cur = root
last = None
if root==None:
return node
while cur!=None:
last = cur
if cur.val>node.val:
cur = cur.left
elif cur.val<=node.val:
cur = cur.right
if last!=None:
if last.val>node.val:
last.left = node
else:
last.right = node
return root
lintcode:在二叉查找树中插入节点的更多相关文章
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- JS中插入节点的方法appendChild和insertBefore的应用
1.appendChild() 方法:可以向节点的子节点列表的末尾添加新的子节点.比如:appendChild(newchild)括号里可以是创建的标签var newchild = document. ...
- C#程序中:如何向xml文件中插入节点(数据)
向xml文件中动态的添加节点(数据)是一件很爽的事,可以给你的程序带来很多的方便,比如在web中,如果你的Flash用到了xml文件,这个方法可以让你在后台就轻轻松松的更新你的Flash内容哦!一起研 ...
- zepto源码--插入节点--学习笔记
与生成width和height使用的方法类似,通过`after`, `prepend`, `before`, `append`,这四者之间的共性,生成对应的函数.并根据这四个函数,生成 `insert ...
- 用OC实现双向链表:构造链表、插入节点、删除节点、遍历节点
一.介绍 双向链表:每一个节点前后指针域都和它的上一个节点互相指向,尾节点的next指向空,首节点的pre指向空. 二.使用 注:跟单链表差不多,简单写常用的.循环链表无法形象化打印,后面也暂不实现了 ...
- 数据结构学习-BST二叉查找树 : 插入、删除、中序遍历、前序遍历、后序遍历、广度遍历、绘图
二叉查找树(Binary Search Tree) 是一种树形的存储数据的结构 如图所示,它具有的特点是: 1.具有一个根节点 2.每个节点可能有0.1.2个分支 3.对于某个节点,他的左分支小于自身 ...
- C#中操作xml文件(插入节点、修改、删除)
已知有一个xml文件(bookstore.xml)如下: <?xml version="1.0" encoding="gb2312"?> <b ...
- lintcode: 二叉查找树中搜索区间
题目 二叉查找树中搜索区间 给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点.找到树中所有值在 k1 到 k2 范围内的节点.即打印所有x (k1 <= x <= ...
- [javaSE] 数据结构(二叉查找树-插入节点)
二叉查找树(Binary Search Tree),又被称为二叉搜索树,它是特殊的二叉树,左子树的节点值小于右子树的节点值. 定义二叉查找树 定义二叉树BSTree,它保护了二叉树的根节点BSTNod ...
随机推荐
- JS中的this用法详解
随着对js的深入学习和使用,你会发现它里面包含了很多令人困惑的机制,比如对象.闭包.原型链继承等等,而这其中肯定包含令你现在或者曾经费解的this,如果你不把心一横,花点时间还真不明白这个this的用 ...
- 解析php开发中的中文编码问题
其实php开发中的中文编码并没有想像的那么复杂,虽然定位和解决问题没有定规,各种运行环境也各不尽然,但后面的原理是一样的. 了解字符集的知识是解决字符问题的基础. PHP程序设计中中文编码问题曾经困扰 ...
- DataSnap中连接池的应用
当开发人员开始创建Delphi的DataSnap应用时很常见的数据库连接定义方式是每个数据模块建立一个连接.这样做将产生大量的数据库连接,并产生很多问题.从Delphi XE开始,EMB提供了Sess ...
- python操作mysql之pymysql
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.7.11.mys ...
- 简单的Datatable转List,Json
这里用到了Newtonsoft.Json,下载地址:http://json.codeplex.com/ 1.根据不同的Model转为对应的List public static List<Mode ...
- ActiveMQ之deliveryMode
在下面的例子中,分别发送一个Persistent和nonpersistent的消息,然后关闭退出JMS. import javax.jms.Connection;import javax.jms.De ...
- BI与大数据
微博的诞生.云计算.物联网.移动互联网等各种爆炸式数据,给商业智能的蓬勃发展提供了良好的“大数据”环境.大数据为BI带来了海量数据.对挖掘来说,大数据量要更容易对比.抢夺大数据市场,需要具备一定的实力 ...
- RaddioButton控件
<GroupBox Margin="5"> <StackPanel> <RadioButton IsChecked="true"& ...
- putty工具常见设置
Putty 工具主要是用于在 windows 环境下连接 linux 服务器的一个命令行工具,可以在此客户端中进行编译.svn代码修改 更新 提交等动作.LD主要是用它来干这个的. 工作环境的改变: ...
- suse linux 操作系统下打BASH补丁
1.检查当前版本信息: bash -version echo $BASH_VERSION 2.打4.3版本的补丁 在tmp目录下(保险起见,空间至少要100M以上)新建一个bash_upgrade ...