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 tree still be a valid binary search tree.
Notice
You can assume there is no duplicate values in this tree + node.
Given binary search tree as follow, after Insert node 6, the tree should be:
2 2
/ \ / \
1 4 --> 1 4
/ / \
3 3 6
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】的更多相关文章
- 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 ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- Unity3d通用工具类之定时触发器
时隔多日,好不容易挤出点时间来写写博文.不容易,请送我几朵红花,点个赞也行. 今天呢,我们主要来扩展下通用工具类==>定时触发器. 顾名思义,所谓的定时触发器,就是告诉程序在过多长时间后,我要执 ...
- vim的批量列编辑
使用快捷键:control+v进入状态“VISUALBLOCK”,然后使用方向键向下选择,选择多行,向右选择,选择多列,使用快捷键d删除所选. 再次进入VISUAL BLOCK状态,选择多行多列, 使 ...
- windows下 memcached 和 redis 服务器安装
memcached 安装: 1.下载memcached 文件: 2.拷贝到运行目录: 3.命令行进入到程序目录: 运行命令: memcached -d install 如果没有报错说明安装成功 4.打 ...
- http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html
http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html http://blog.sina.com.cn/s/blog_538 ...
- asp.net 大文上传配置
配置iis的文件上传大小控制: 今天先说一个问题,在window server2008 下配置上传文件的大小,除了工程自身的config外还需要配置一下服务器上的applicationhost.con ...
- 配置thinkphp3.2 404页面
ThinkPHP自身提供了 404 页面的处理机制,我们只需要在控制器 中添加一个 EmptyController.class.php,并且实现以下方法即可,方法如下: <? class Em ...
- 第七章:JAVA引用ESWCF及部分方法说明
WCF搭建地址: http://127.0.0.1:8081/Search.svc?wsdl 操作如下: 引用webservice 2.输入webservice访问地址,点击下一步 3.点击Finis ...
- elastic不错的官方文档(中文)
https://www.blog-china.cn/template/documentHtml/1484101683485.html http://www.open-open.com/doc/list ...
- redis学习笔记——RDB和AOF持久化二
上一篇对RDB的源码分析是比较多的,但是AOF持久化执行进行了一些理论上的分析和概念的说明.本来想自己偷一些懒,将上篇文章中最后所给链接的AOF实现代码随便过一过算了,后来也就是在过的过程中发现自己这 ...
- dubbo-monitor安装监控中心,管理控制台安装
一.安装监控中心 1.创建安装目录 2.解压 上传文件解压文件 解压 3.修改配置文件 4.启动 如果一直出现点.只需要加大内存即可,内存至少大于1024,然后reboot重启 5.测试 二.安装管理 ...