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. MIME类型列表

    A Multipurpose Internet Mail Extensions (MIME) type is a standard that indicates the nature and form ...

  2. syslinux 和 grub

    syslinux是一个功能强大的引导加载程序,而且兼容各种介质.它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘.它的安装很简单,一旦安装syslinux好之后,sysLinu ...

  3. isolinux.cfg 文件是干什么的

    1.   首先光盘镜像也就是iso文件采用的是“ISO 9660 ”文件系统 . cd上的文件都存在这个简单的iso文件系统里,linux可以用mount  -o loop 直接把*.iso文件mou ...

  4. Unity3D Android动态反射加载程序集

    这种办法在iOS下是不让用的,只能在Android下用.用起来也很方便了. 1.先创建一个c#工程,引用到的UnityEngine.dll在Unity的安装目录里找吧 2.将编译的dll放入Unity ...

  5. pycharm的版本问题

    1.分类: 专业版是收费的 Professional 教育版是免费 edu https://www.jetbrains.com/pycharm-edu/whatsnew/ 社区版是免费的 Free C ...

  6. 基于spring-cloud的微服务(3)eureka的客户端如何使用IP地址来进行注册

    例子中和我写的代码里,使用的spring-boot的版本是2.0 Eureka的客户端默认是使用hostname来进行注册的,有的时候,hostname是不可靠的,需要使用IP地址来进行注册 name ...

  7. 使用spring提供的ReflectionUtils简化项目中反射代码的复杂性

    在项目中有时候我们会使用到反射的功能,如果使用最原始的方法来开发反射的功能的话肯能会比较复杂,需要处理一大堆异常以及访问权限等问题.spring中提供了ReflectionUtils 这个反射的工具类 ...

  8. docker link 过时不再用了?那容器互联、服务发现怎么办?

    在 1-2 年前,Docker 所有容器都连接于默认的桥接网络上,也就是很多老文章鼓捣的 docker0 桥接网卡.因此实际上默认情况下所有容器都是可以互联的,没有隔离,当然这样安全性不好.而服务发现 ...

  9. 如何玩facebook app上的H5游戏

    一.下载facebook app facebook链接 二.下载vpn 国内是访问不了的,必须vpn 因为vpn被封了厉害 我随便百度了个狸猫加速器,没付费,时断时续... 没有推荐的vpn,哪位大佬 ...

  10. C++ 输入/输出

    std:: 是什么?有什么作用? 输入和输出的iostream 库.iostream 库的基础是两种命名为 istream 和 ostream 的类型,分别表示输入流和输出流. 标准库定义了 4 个 ...