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.

Notice

You can assume there is no duplicate values in this tree + node.

 
Example

Given binary search tree as follow, after Insert node 6, the tree should be:

  2             2
/ \ / \
1 4 --> 1 4
/ / \
3 3 6
Challenge

Can you do it without 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) {
if (root == null) {
return node;
}
if (root.val > node.val) {
root.left = insertNode(root.left, node);
} else {
root.right = insertNode(root.right, node);
}
return root;
}
}

解法二:

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

解法三:

 class Solution {
public:
/*
* @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.
*/
TreeNode * insertNode(TreeNode * root, TreeNode * node) {
stack<TreeNode * > sta;
sta.push(root);
TreeNode * last = NULL;
while (sta.size() != ) {
TreeNode * now = sta.top();
sta.pop();
if (now == NULL) {
if (last == NULL) {
root = node;
} else if (last->val <= node->val) {
last -> right = node;
} else {
last -> left = node;
}
break;
} else if (now->val <= node->val) {
sta.push(now->right);
} else {
sta.push(now->left);
}
last = now;
}
return root;
}
};

参考@DLNU-linglian 的代码

85. Insert Node in a Binary Search Tree【easy】的更多相关文章

  1. Lintcode: Insert Node in a Binary Search Tree

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

  2. 606. Construct String from Binary Tree 【easy】

    606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...

  3. 501. Find Mode in Binary Search Tree【LeetCode by java】

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  4. 543. Diameter of Binary Tree【Easy】【二叉树的直径】

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

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

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

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

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

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

随机推荐

  1. html模板与json数据交互

    阅读这篇文章后,对html和json有很大的启发: http://www.zhangxinxu.com/wordpress/2012/09/javascript-html-json-template/ ...

  2. windows2012 IIS部署GeoTrust证书踩过的坑。

    系统:windows2012 环境:IIS8 在阿里云上买了GeoTrust证书, 按照说明下载证书到服务器,  导入证书, 给IIS站点部署https. 阿里云部署帮助文档:https://help ...

  3. OCP试题解析之053-17 CONFIGURE CONTROLFILE AUTOBACKUP ON

    17.You configure AUTOBACKUP to ON in an RMAN session. When will RMAN back up the control file? (Choo ...

  4. python中常用的推导(字典推导和列表推导)

    在python开发中经常需要书写这样的代码 result = list() for data in datas: if data not in ['a', 'b']: result.append(da ...

  5. 流畅的python第五章一等函数学习记录

    在python中,函数是一等对象,一等对象是满足以下条件的程序实体 1在运行时创建 2能复制给变量或数据结构的元素 3能作为参数传给函数 4能作为函数的返回结果 高阶函数(接受函数作为参数或者把函数作 ...

  6. 构建高可用Linux服务器二

    centos优化: 1.关闭不需要的服务:ntsysv crond:自动计划任务 network:已配置网络接口的脚步程序服务. sshd:openssh服务器守护进程. irqbalance:启用i ...

  7. MySQL数据库如何与EXCEL的XLS格式相互转换

    1 将SQL导出为EXCEL方法,有如下数据库(my_impa),里面有两张表 2 如果是EXCEL格式,一定要勾选"将字段名称放在首行",否则待会儿导入的时候就需要你手工新建字段 ...

  8. jsp实现仿QQ空间新建多个相冊名称,向相冊中加入照片

    工具:Eclipse,Oracle,smartupload.jar.语言:jsp,Java:数据存储:Oracle. 实现功能介绍: 主要是新建相冊,能够建多个相冊,在相冊中加入多张照片,删除照片,删 ...

  9. COSMOSBOX手遊制作手册(Word备份)

    20140712版 版本号 Version 日期 Date 作者 Author 变更主要原因描述 Brief Description 1.0 2014-4-26 陈霈霖 初稿 2.0 1. 前言 本手 ...

  10. 【Firefly API文档】—— Package DBentrust

    http://bbs.gameres.com/thread_219653_1_1.html package dbentrust 该包下面主要是数据库的处理与memcached存储.里面封装了,从mem ...