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. 跟我一起写Makefile:使用函数

    跟我一起写Makefile:使用函数 两个排版不一样 书籍下载 书籍下载

  2. jenkins部署war包到远程服务器的tomcat

    一.目的 jenkins上将war包,部署到远程服务器的tomcat上. 这边tomcat在windows 主机A上,版本apache-tomcat-8.5.23. jenkins在主机B上,cent ...

  3. docker自动开启端口转发功能

    yum -y install epel-release yum -y install docker-io service docker start docker pull haproxy # 此时自动 ...

  4. Jdk提供的动态代理示例

    package com.jiaoyiping.util.demo; import java.lang.reflect.InvocationHandler; import java.lang.refle ...

  5. PHP服务器访问优化

    常规的优化措施: 磁盘写入,网络安全,证书加密,CPU,内存,DNS解析,数据库优化,页面gzip压缩 PHP gzip压缩打开: 打开php目录下的php.ini文件,找到zlib.output_c ...

  6. python nose测试框架全面介绍六--框架函数别名

    之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.pa ...

  7. mac操作

    资料搜集: mac终端 常用命令操作 mac osx常用快捷键一览 mac chrome快捷键

  8. dinner vs supper

    用dinner还是supper? 据我唯一认识一个美国人……讲,至少在美国他们用dinner,supper也许在英国更常用些. 他在小时候都没听说过supper这个词…… 另外,have dinner ...

  9. Java与JS判断请求来是否来自移动端

    Java public boolean JudgeIsMoblie(HttpServletRequest request) { boolean isMoblie = false; //String[] ...

  10. cURL 是一个功能强大的PHP库。

    使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了.无论是你想从从一个链接上取部分数据,或是取一个XML文件并把 ...