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 the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
For example,
Given the tree:
4
/ \
2 7
/ \
1 3
And the value to insert: 5
You can return this binary search tree:
4
/ \
2 7
/ \ /
1 3 5
This tree is also valid:
5
/ \
2 7
/ \
1 3
\
4
Approach #1: C++.[rescursive]
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (root == nullptr) root = new TreeNode(val);
if (root->val > val) root->left = insertIntoBST(root->left, val);
if (root->val < val) root->right = insertIntoBST(root->right, val);
return root;
}
};
Approach #2: Java.[iterator]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) root = new TreeNode(val);
TreeNode cur = root;
while (true) {
if (cur.val <= val) {
if (cur.right != null) cur = cur.right;
else {
cur.right = new TreeNode(val);
break;
}
} else {
if (cur.left != null) cur = cur.left;
else {
cur.left = new TreeNode(val);
break;
}
}
}
return root;
}
}
Approach #3: Python.[recursive]
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def insertIntoBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
if not root:
return TreeNode(val);
if root.val < val:
root.right = self.insertIntoBST(root.right, val)
elif root.val > val:
root.left = self.insertIntoBST(root.left, val) return root
Insert into a Binary Search Tree的更多相关文章
- [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 ...
- 【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】701. Insert into a Binary Search Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode题解之Insert into a Binary Search Tree
1.题目描述 2.分析 插入算法. 3.代码 TreeNode* insertIntoBST(TreeNode* root, int val) { insert(root, val); return ...
- leetcode701. Insert into a Binary Search Tree
https://www.cnblogs.com/grandyang/p/9914546.html 类似于二分查找的方法,用迭代的方法去做 注意:无论是进入左子树还是右子树,左右子树都变成了新的数,所以 ...
- [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...
- 算法与数据结构基础 - 二叉查找树(Binary Search Tree)
二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...
随机推荐
- 《高性能Javascript》 Summary(一)
第一章.加载和执行 Loading & Execution 原因:Javascript 的执行导致页面渲染中止等待. 解决: 将script放在页面底部,紧靠body 闭合标签之前,保证页面在 ...
- codeforces 之 Little Pigs and Wolves
B-Little Pigs and Wolvestime limit per test2 secondsmemory limit per test256 megabytesinputstandard ...
- 51nod【1196】字符串的数量
超级神题! 有n种字符,若此种字符的编号( \(1\) ~ \(n\)),\(i*2>n\),则他后面可接任意字符.若不是,则他后面接的字符编号至少要是他的两倍. 问长度为m的字符串的个数. 这 ...
- IDT 信息设计工具使用
IDT 大多使用于接外系统数据,也就是非SAP 系统数据库.虽然SAP BW 里有数仓概念,接外部系统的视图.集中在使用建模,query ,再通过BO来展示.可是有些我们需要直连数据库,取一两张表相对 ...
- hdu-5656 CA Loves GCD(dp+数论)
题目链接: CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- 四连测Day4
四连爆炸 卡我常数 好像被AluminumGod拉到了创客...哇我这个天天爆炸的水平可能会被其他三位dalao吊起来打 orz Edmond-Karp_XiongGod orz Deidara_Wa ...
- [Codeforces 452E] Three Strings
[题目链接] https://codeforces.com/contest/452/problem/E [算法] 构建后缀数组 用并查集合并答案即可 时间复杂度 : O(NlogN) [代码] #in ...
- dataguard 下主备 online redo 与 standby redo log resize 重建
环境说明: 本实验环境是一个节点的rac + 单节点 asm dg database 与 grid 版本是 11.2.0.4 .提别提醒 如果是多节点集群,操作时需要特别注意 thread . ...
- scala新人佑门
scala语言 1. val和var val和var当前区别在于前者只能被赋值一次,就像java中的final,但是后者则可以随意覆盖: 2. Unit Unit返回代表空返回,类似于vo 3. 类型 ...
- 洛谷 2585 [ZJOI2006]三色二叉树——树形dp
题目:https://www.luogu.org/problemnew/show/P2585 可以把不是绿色的记成一种.仔细一想不会有冲突.如果自己是绿色,孩子的不同颜色不会冲突:如果自己不是绿色,自 ...