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 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的更多相关文章
- 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 ...
- LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height
C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...
- lintcode 中等题:unique Binary Search Tree 不同的二叉查找树
题目 不同的二叉查找树 给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种? 样例 给出n = 3,有5种不同形态的二叉查找树: 1 3 3 2 1 \ / / / \ \ 3 2 1 ...
- 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 ...
- 【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 ...
随机推荐
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
- 基于Python的跨平台端口转发工具
背景 使用lcx也好,nc也好,总是会被安全防护软件查杀,所以想着自己写一个.顺面学习一下,端口转发的原理. 端口转发的逻辑 端口转发的逻辑很简单开启两个scoket,一个绑定IP端口进行listen ...
- PCB常见的拓扑结构 (转)
常见的拓扑结构有: 1.点对点拓扑 point-to-point scheduling 该拓扑结构简单,整个网络的阻抗特性容易控制,时序关系也容易控制,常见于高速双向传输信号线:常在源端加串行 ...
- java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder
java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder 少导入了jdom.jar包. 下载地址:http://www.jdom.org/do ...
- yii---进行增删改查
我们使用yii进行数据的增删改查: 一.新增数据 使用model::save()操作进行新增数据 $user= new User; $user->username =$username; $us ...
- 170809、 把list集合中的数据按照一定数量分组
/** * @Desc : 切分list位多个固定长度的list集合(我这是业务需要,直接是1w条数据切分) * @Author : RICK * @Params: [historyList] * @ ...
- 搭建本地DNS解析
一.安装Dnsmasq yum install -y dnsmasq 二.安装dig yum install dnsmasq -y 三.编辑配置文件 vim /etc/dnsmasq.conf res ...
- TFS二次开发08——分支(Branch)和合并(Merge)
一:创建分支 private static void BranchFile(Workspace workspace, String newFilename) { String branchedFi ...
- 被included或者被required的文件都来自哪里呢
过PHP,你可以使用不同函数帮助你重用代码.具体用到的函数取决于你打算重用的内容. 主函数如下: * include() and include_once() * require() and requ ...
- Ubuntu下更改Vim配置文件打造C/C++风格
转载:Ubuntu下更改Vim配置文件打造C/C++风格 Vim默认的配置使用起来还不能让人满意,还需要自己配置 默认配置文件是:/etc/vim/vimrc我们可以在家目录下建立自己的配置文件切换到 ...