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
Given binary search tree as follow: 2 / \ 1 4 / 3 after Insert node 6, the tree should be: 2 / \ 1 4 / \ 3 6 Challenge
Do it without recursion

Recursion做法:

 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 (node == null) return root;
helper(root, node);
return root;
} public void helper(TreeNode root, TreeNode node) {
if (root.val <= node.val && root.right == null) root.right = node;
else if (root.val > node.val && root.left == null) root.left = node;
else if (root.val <= node.val) helper(root.right, node);
else helper(root.left, node);
}
}

Iterative做法:

 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 (node == null) return root;
TreeNode rootcopy = root;
while (root != null) {
if (root.val<=node.val && root.right==null) {
root.right = node;
break;
}
else if (root.val>node.val && root.left==null) {
root.left = node;
break;
}
else if(root.val <= node.val) root = root.right;
else root = root.left;
}
return rootcopy;
}
}

Lintcode: Insert Node in a Binary Search Tree的更多相关文章

  1. 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 ...

  2. LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height

    C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...

  3. lintcode 中等题:unique Binary Search Tree 不同的二叉查找树

    题目 不同的二叉查找树 给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种? 样例 给出n = 3,有5种不同形态的二叉查找树: 1 3 3 2 1 \ / / / \ \ 3 2 1 ...

  4. 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 n ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. adb 查看内存信息的命令

    meminfo: basic memory status-adb shell cat proc/meminfo  -- 内存系统信息-adb shell cat proc/pid/maps --  指 ...

  2. linux中的标准输出和输入

    ===============1.有些人经常问我这个问题问题=========== 经常在脚本里面看到这个    2>&1     表示什么意思啊? ==============2.理论 ...

  3. 监控redis服务器执行的命令--类似于tomcat的local-access.log

    一.问题由来 一般程序启动时会去拉必要的缓存存进去redis. 由于我们这边开发可直连开发和测试环境,有时候会发生,开发同学本地直连了测试环境,本地ide一启动,可能会导致重新覆盖了测试环境上的缓存. ...

  4. Linux下使用 xrandr 命令设置屏幕分辨率

    最近在Linux下修改屏幕分辨率的时候,发现了一个非常有用的命令:xrandr 使用这个命令,可以方便的设置您显示器的的分辨率.尤其是当你使用了一些需要或者会自动改动您屏幕分辨率的程序以后. 您可以使 ...

  5. VS2003安装Opencv1.0 windows系统 win7

    一.步骤 下载安装opencv1.0     安装文件我上传到百度网盘分享连接 http://pan.baidu.com/s/1o8na0aA 配置电脑windows环境变量 配置VS2003全局设置 ...

  6. [通信] C#多线程Socket-文件传输

    FileSendClient : Form1.cs using System; using System.IO; using System.Net; using System.Net.Sockets; ...

  7. C语言中的数组的使用——混乱的内存管理

    在C语言中想要创建数组只能自己malloc或者calloc,数组复制则是memcpy. 这样创建出来的数组在调用时是不会检测数组边界的,即你声明了一个长度为5的数组,却可以访问第6个位置……也可以给第 ...

  8. thinkphp---部署在IIS8.0服务器上

    最近做了一个项目,使用的是我自己基于thinkphp开发的一套CMS,由于我本地使用的都是apche的环境,即使是线上环境用的也是宝塔面板,但是现在要将thinkphp的系统部署在IIS8.0的环境下 ...

  9. vue--动态路由和get传值

    动态路由: <template> <div id="News"> <v-header></v-header> <hr> ...

  10. 生存分析(survival analysis)

    一.生存分析(survival analysis)的定义 生存分析:对一个或多个非负随机变量进行统计推断,研究生存现象和响应时间数据及其统计规律的一门学科. 生存分析:既考虑结果又考虑生存时间的一种统 ...