题目:

 在二叉查找树中插入节点

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

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

 样例

给出如下一棵二叉查找树,在插入节点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. Bundle、Intent、SharedPreferences

    Intent与Bundle的共同点:都继承Parcelable Intent传值与Bundle传值的区别 eg:我现在要从A界面   跳转到B界面或者C界面   这样的话 我就需要写2个Intent  ...

  2. c# WinForm 编程总结

    1.清空DataGridView /// <summary> /// 清空DataGridView /// </summary> /// <param name=&quo ...

  3. git服务器简易搭建法

    受尽svn各种折磨的小伙伴都听说过git. 但是网上一搜索, 本来打算跟svn一样. 下一个服务器, 装下就可以开始用了. 没想到啊, 没想到. 居然需要Linux天赋点… 好吧, 拜鸟哥门下把Lin ...

  4. c#读写注册表示例分享

    c#读写注册表示例,示例中有详细注释. 代码: //写注册表RegistryKey regWrite;//往HKEY_CURRENT_USER主键里的Software子键下写一个名为“Test”的子键 ...

  5. Win2012 R2 IIS8.5+PHP(FastCGI)+MySQL运行环境搭建教程

    这篇文章主要介绍了Win2012 R2 IIS8.5+PHP(FastCGI)+MySQL运行环境搭建教程,需要的朋友可以参考下 准备篇 一.环境说明: 操作系统:Windows Server 201 ...

  6. MemProof教程

    简介 MemProof(内存清道夫)是AutomatedQA出品的一款非常不错的检测内存泄漏和资源泄漏的免费调试工具,适合于WIN32平台下使用DELPHI/C++ BUILDER开发的应用程序. 利 ...

  7. C语言中的字符串拷贝函数strcpy和内存拷贝函数memcpy的区别与实现

    strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符'\0'. 已知st ...

  8. 【笔记】UML核心元素

    1.参与者 定义:在系统之外与系统交互的某人或某物. 特点:1.可以非人:2.与系统直接交互:3.主动发出动作并获得反馈:4.涉众(stakerholder)的代表 具有两个版型: 1.业务主角(bu ...

  9. bzoj 4010: [HNOI2015]菜肴制作 拓扑排序

    题目链接: 题目 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory Limit: 512 MB 问题描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴 ...

  10. EXT经验--在调试中通过查看handler的第一个参数的xtype得知该参数信息及该handler的归属

    EXT模拟了OPP的思想,因此很多问题可以像JAVA语音那样去思考它.在实际阅读EXT时,常常需要我们搞清楚某个函数.某个对象的归属.如某个参数变量.方法属于哪个类,如下: 这是我今天在群中发出的问题 ...