题目:

 在二叉查找树中插入节点

给定一棵二叉查找树和一个新的树节点,将节点插入到树中。

你需要保证该树仍然是一棵二叉查找树。

 样例

给出如下一棵二叉查找树,在插入节点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:在二叉查找树中插入节点的更多相关文章

  1. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  2. JS中插入节点的方法appendChild和insertBefore的应用

    1.appendChild() 方法:可以向节点的子节点列表的末尾添加新的子节点.比如:appendChild(newchild)括号里可以是创建的标签var newchild = document. ...

  3. C#程序中:如何向xml文件中插入节点(数据)

    向xml文件中动态的添加节点(数据)是一件很爽的事,可以给你的程序带来很多的方便,比如在web中,如果你的Flash用到了xml文件,这个方法可以让你在后台就轻轻松松的更新你的Flash内容哦!一起研 ...

  4. zepto源码--插入节点--学习笔记

    与生成width和height使用的方法类似,通过`after`, `prepend`, `before`, `append`,这四者之间的共性,生成对应的函数.并根据这四个函数,生成 `insert ...

  5. 用OC实现双向链表:构造链表、插入节点、删除节点、遍历节点

    一.介绍 双向链表:每一个节点前后指针域都和它的上一个节点互相指向,尾节点的next指向空,首节点的pre指向空. 二.使用 注:跟单链表差不多,简单写常用的.循环链表无法形象化打印,后面也暂不实现了 ...

  6. 数据结构学习-BST二叉查找树 : 插入、删除、中序遍历、前序遍历、后序遍历、广度遍历、绘图

    二叉查找树(Binary Search Tree) 是一种树形的存储数据的结构 如图所示,它具有的特点是: 1.具有一个根节点 2.每个节点可能有0.1.2个分支 3.对于某个节点,他的左分支小于自身 ...

  7. C#中操作xml文件(插入节点、修改、删除)

    已知有一个xml文件(bookstore.xml)如下: <?xml version="1.0" encoding="gb2312"?> <b ...

  8. lintcode: 二叉查找树中搜索区间

    题目 二叉查找树中搜索区间 给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点.找到树中所有值在 k1 到 k2 范围内的节点.即打印所有x (k1 <= x <= ...

  9. [javaSE] 数据结构(二叉查找树-插入节点)

    二叉查找树(Binary Search Tree),又被称为二叉搜索树,它是特殊的二叉树,左子树的节点值小于右子树的节点值. 定义二叉查找树 定义二叉树BSTree,它保护了二叉树的根节点BSTNod ...

随机推荐

  1. JavaScript创建对象的写法

    JavaScript 有Date.Array.String等这样的内置对象,功能强大使用简单,人见人爱,但在处理一些复杂的逻辑的时候,内置对象就很无力了,往往需要开发者自定义对象.   对象是什么 从 ...

  2. php基础小知识

    1.php中的双引号可以正确的解析变量与转义序列,而单引号只会按照声明原样显示:双里面的字段会经过编译器解释,然后再当作HTML代码输出:单引号里面的不进行解释,直接输出. 2.转义序列是针对源代码的 ...

  3. php正则表达式总结第1弹

    介绍几个我用到的php正则表达式 1. 一篇文章的链接,我需要去掉以 /hotels/打头的链接,可用下面正则 $content = preg_replace('/<a(.*?)href=&qu ...

  4. DataSnap如何监控Tcp/IP客户端的连接情况

    一个实例,如果客户端是TCP/IP是短连接的情况就没有必要了. 一.GlobVar.pas单元,定义应用系统全局数据类型及变量: unit GlobVar; interface uses System ...

  5. Python中处理时间 —— time模块

    time模块 time 模块可用来处理时间,详细的说明参考 time模块说明. 逝去的秒数 逝去的秒数表示从某个时间(Python中是"Thu Jan 1 07:00:00 1970&quo ...

  6. 1094. The Largest Generation (25)

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  7. ActiveMQ.xml文件的主要配置

    ActiveMQ.xml文件默认位置位于 activemq/conf/目录下,主要的配置及解析如下:<beans xmlns="http://www.springframework.o ...

  8. mysqldump 参数说明

    mysqldump参数说明 --all-databases , -A 导出全部数据库. mysqldump -uroot -p --all-databases --all-tablespaces , ...

  9. winrar 压缩文件方法

    问题描述: 我要一些大的文件进行压缩,看了网上有很多类拟的写法.但在我这都存在两个问题. 1.就是他们都是通过注册表找到rar的exe目录.我安装好winrar后,虽然注册表有,但那个目录一直报一个错 ...

  10. WPF中使用MVVM模式进行简单的数据绑定

    计划慢慢整理自己在WPF学习和工作应用中的一些心得和想法,先从一个简单的用法说起 在WPF中,XAML标记语言中绑定数据,而数据源就是指定为ViewModel类,而非界面本身的逻辑代码类 这样一定程度 ...