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 ...
随机推荐
- Scrapy源码学习(一)
用Scrapy已经有一段时间了,觉得该是看一下源码的时候了.最开始用的时候还是0.16的版本,现在稳定版已经到了0.18.结合使用Scrapy的过程,先从Scrapy的命令行看起. 一.准备 下载源代 ...
- trade 主要前端组件
jQuery Custombox http://www.jqueryfuns.com/resource/view/27
- hdu 5690 2016"百度之星" - 初赛(Astar Round2A) All X 快速二次幂 || 寻找周期
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5690 题意:m个数字全为x mod k ?= c;其中m <= 1010,0 < c,k ...
- static与全局与局部变量的区别
转自:http://www.cnblogs.com/lzjsky/archive/2010/11/19/1882064.html 全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变 ...
- glog使用
How To Use Google Logging Library Glog 的基本使用方法在google code上有介绍:How To Use Google Logging Library ;最好 ...
- owin 中间件 katana 如何解密cookie
.NET MVC5 默认的用户登录组件是AspNet.Identity ,支持owin,并且微软自己实现的一套owin 中间件叫 katana 补充一下 katana项目源码地址:https://ka ...
- .NET基础之--C#中判断空字符串的3种方法性能分析
那么为什么if(a.Length==0)最快呢?因为整数判断等于最快,没有经过实例化等复杂的过程. 所以:建议大家判断字符串是否为空用 if(a.Length==0). 对于三种方法的评价: 1.if ...
- 深度分析 Java 的 ClassLoader 机制(源码级别)
写在前面:Java中的所有类,必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器完成的,类装载器所做的工作实质是把类文件从硬盘读取到内存中,JVM在加载类的时候,都是通过ClassLoa ...
- c#之反射总结
1.了解什么事程序集 2.加载程序集 首先要加载需要加载的程序集,然后找到指定的类型,进而往下进行动态加载. 要加载的程序集中的内容: public class Class1:Person { pr ...
- WPF解析PPT为图片
偶遇需要解析 PPT为单张图片 其中,对于包含动画的PPT页,分别对动画最后效果进行截取,即每个连续动画截取 (动画N个)N+1(原图)张 http://git.oschina.net/jiailiu ...