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 ...
随机推荐
- 【hibernate】错误:org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was altered from xx to xx
所报错误: org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was ...
- 统一D3D与OpenGL坐标系统
作者:游蓝海(http://blog.csdn.net/you_lan_hai) DirectX 3D与OpenGL坐标系统的差异性,给我们带来非常大的麻烦.让跨平台编程的新手非常困惑.近期在做一个跨 ...
- API Monitor---------------Using API Monitor to crack copy protected software
For this tutorial we will be using Mirial Softphone which is a HD video conferencing application. Th ...
- 在Ubuntu Server上源码安装OpenERP 8.0,并配置wsgi和nginx运行环境
原文: How to install OpenERP 8.0 Alpha on a fresh Debian / Ubuntu server. OpenERP的安装,可以有多种方式,通过添加源,到 h ...
- 内向者沟通圣经:4P法(Preparation,Presence,Push,Practice)
一.对于内向者的态度 坦白,坦白也许是个不错的方法,内向不是缺点,只是性格: 拥抱和全面衡量你自己,无论内向还是外向:(我觉得无论是内向还是外向,都应该这么做) 当你无法与自身的思想和平共处,你们开始 ...
- 异常解决:util.NativeCodeLoader: Unable to load native-hadoop library for your platform
内容源自:点此链接 刚装好hadoop的时候,每次输入命令运行都会出现: WARN util.NativeCodeLoader: Unable to load native-hadoop librar ...
- const 与 指针
#include <iostream> using namespace std; int main() { // 第一种.使指针不能改动对象的值.注:此时指针能够指向另外的对象 int i ...
- swift 使用匿名函数初始化属性
swift 使用匿名函数初始化属性 匿名函数格式: { //代码体 }() 大括号是定义函数体的,小括号()是调用函数体的. 为什么不写一个函数然后调用函数呢?为何用匿名函数呢? 主要还是代码的简洁性 ...
- IM开发基础知识补课(四):正确理解HTTP短连接中的Cookie、Session和Token
本文引用了简书作者“骑小猪看流星”技术文章“Cookie.Session.Token那点事儿”的部分内容,感谢原作者. 1.前言 众所周之,IM是个典型的快速数据流交换系统,当今主流IM系统(尤其移动 ...
- nodejs之SVG转图片下载方案
本文介绍在nodejs基础上.怎样实现将svg转为png并下载的功能. 所需Webkit和node module简单介绍: phantomjs:一个基于WebKit的server端JavaScript ...