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)或递归 ...
随机推荐
- mac 在 finder 当前 路径下 打开 terminal 的办法
1. 在:系统偏好设置 -> 键盘 -> 服务 或者 finder -> 服务偏好设置, 如下: 建议配合快捷键使用,本人使用的快捷键: 在 terminal 新建标签 contro ...
- Maven简介(六)——Dependency
7 Dependency介绍 http://elim.iteye.com/category/269897 7.1 依赖的传递性 当项目A依赖于B,而B又依赖于C的时候,自然的A会依赖 ...
- 分享知识-快乐自己:2017IDEA破解教程
首先 修改host文件: 文件路径:C:\Windows\System32\drivers\etc\hosts 修改:将“0.0.0.0 account.jetbrains.com”追加到hosts文 ...
- python- 常见算法 python内置模块
1.冒泡排序 需求:请按照从小到大对列表 [13, 22, 6, 99, 11] 进行排序 原理:相邻两个值进行比较,将较大的值放在右侧,依次比较! li=[39,11,43,88,765,9]for ...
- Mybatis中的like模糊查询
1. 参数中直接加入%% param.setUsername("%CD%"); param.setPassword("%11%"); <sel ...
- linux ssh 命令使用解析
前一阵远程维护Linux服务器,使用的是SSH,传说中的secure shell. 登陆:ssh [hostname] -u user 输入密码:***** 登 陆以后就可以像控制自己的机器一样控制它 ...
- arm-linux-gcc4.4.3编译s3c2410平台linux内核
写在前面:2.6.14版本的内核用arm-linux-gcc4.4.3没有编译成功,下载2.6.37版本的内核用arm-linux-gcc4.4.3编译通过. 一.首先下载linux内核: linux ...
- (转)[sh] <defunct>进程
我也碰到僵尸进程了,必须通过关闭僵尸进程的父进程或者重启解决. 来自:http://jerrybear.blog.51cto.com/629421/387846 今日发现服务器上多了一些名称为[sh] ...
- 集训Day11
别人的题公开原题面不好 写题解吧 T1 很明显答案满足二分,二分之后算出每个人的位置,做一个LIS即可 T2 阅读体验极差 T3 根本不会 交都没交 期望得分200
- HiHoCoder1671 : 反转子串([Offer收割]编程练习赛41)(占位)
描述 给定一个只包含括号和小写字母的字符串S,例如S="a(bc(de)fg)hijk". 其中括号表示将里面的字符串翻转.(注意括号可能嵌套) 请你输出翻转之后的字符串. 输入 ...