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. axure rp ----专业的快速原型设计工具

    Axure RPAxure的发音是』Ack-sure』,RP则是』Rapid Prototyping』快速原型的缩写.Axure RP Pro是美国Axure Software Solution公司的 ...

  2. iOS - 视频播放处理全屏/横屏时候遇见的坑

    视频播放想要全屏,使用shouldAutorotate方法禁止主界面,tabbar控制器横屏,导致push进入播放页面不能横屏的问题... - (BOOL)shouldAutorotate { ret ...

  3. python-django开发学习笔记四

    先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...

  4. VS2015:出现devenv.sln解决方案保存对话框

    问题描述: 打开VS2015项目时,提示保存“devenv.sln” 解决方法: 找到文件:C:\Program Files (x86)\Common Files\microsoft shared\M ...

  5. .NET中的类型对象

    .NET中的任何类型,都有对应的一个类型对象.类型对象和类型实例(类型创建的一个对象)不是同一个概念. 类型对象包含类型的静态字段和方法,当类访问静态方法静态字段,实例调用方法时就会去类型对象中查找静 ...

  6. Pandas的concat方法

    在此我用的concat作用是加入新的记录,存储数据来用过的,不知道数据量大时候,效率会怎样 # 使用pandas来保存数据 df1 = pd.DataFrame([poem], columns=['p ...

  7. 【紫书】Oil Deposits UVA - 572 dfs求联通块

    题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...

  8. asm 32 /64

    我使用NASM编写的,运行在32位windows和linux主机上,但后来需求增加了,需要在64位windows和linux上运行,windows自身有个wow(windows on windows) ...

  9. MyBatis返回插入的主键ID(Mysql数据库)

    1.Java代码: 1.1 entity类: User.java public class User { private int userId; private String userName; pr ...

  10. Highways---poj1751最小生成树

    http://poj.org/problem?id=1751 题意:有n个点,已知各点坐标,距离为权值,求最小生成树的边 但是这个最小生成树的m条边是已经确定的了,所以可以让已知边的权值为0: 在Pr ...