当前位置:网站首页>【LeetCode】701.二叉搜索树中的插入操作
【LeetCode】701.二叉搜索树中的插入操作
2022-08-04 10:50:00 【酥酥~】
题目
给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。
示例 1:
输入:root = [4,2,7,1,3], val = 5
输出:[4,2,7,1,3,5]
解释:另一个满足题目要求可以通过的树是:
示例 2:
输入:root = [40,20,60,10,30,50,70], val = 25
输出:[40,20,60,10,30,50,70,null,null,25]
示例 3:
输入:root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
输出:[4,2,7,1,3,5]
提示:
树中的节点数将在 [0, 104]的范围内。
-108 <= Node.val <= 108
所有值 Node.val 是 独一无二 的。
-108 <= val <= 108
保证 val 在原始BST中不存在。
题解
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if(root == nullptr)
return new TreeNode(val);
TreeNode* node = root;
while(node)
{
if(val < node->val)
{
if(node->left)
node = node->left;
else
{
node->left = new TreeNode(val);
break;
}
}
else
{
if(node->right)
node = node->right;
else
{
node->right = new TreeNode(val);
break;
}
}
}
return root;
}
};
递归
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if(root == nullptr)
return new TreeNode(val);
if(val < root->val)
root->left = insertIntoBST(root->left,val);
else
root->right = insertIntoBST(root->right,val);
return root;
}
};
边栏推荐
- datax oracle to oracle incremental synchronization
- 3-5年以上的功能测试如何进阶自动化?
- RAID介绍及RAID5配置实例
- There are 12 balls, including 11 weight, only one, don't know is light or heavy. Three times in balance scales, find out the ball.
- LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三
- 【机器学习】:如何对你的数据进行分类?
- 美摄问答室|美映 VS 美摄云剪辑
- 【虹科案例】基于3D相机组装家具
- 标准C语言学习总结12
- Events in August | 51CTO's 17th Anniversary Celebration, post a blog post to get gifts such as tea sets/notebooks/T-shirts!
猜你喜欢
![[Hongke case] Assembling furniture based on 3D camera](/img/00/bd04f9445add2571ad9cf276e81cb1.png)
[Hongke case] Assembling furniture based on 3D camera

Graphical Hands-on Tutorial--ESP32 One-Key Network Configuration (Smartconfig, Airkiss)

map的一道题目<单词识别>

mae,mse,rmse分别利用sklearn和numpy实现

Meishe Q&A Room | Meiying VS Meishe Cloud Editing

第二批养老理财试点产品发行 一小时销售20亿元

MySQL:面试问的范式设计

Apache Calcite 框架原理入门和生产应用

八、MFC对话框

CVPR 2022 | 从人体网格预测骨架,是真正的生理学骨架!
随机推荐
怎么禁止textarea拉伸
江西发布紧急通知:全面开展涉校涉生安全隐患大排查
ThreadLocal详细分析
Win11文件类型怎么改?Win11修改文件后缀的方法
黑马瑞吉外卖之员工账号的禁用和启用以及编辑修改
RL78开发环境
Introduction to the core methods of the CompletableFuture interface
广东对小鹏/广汽丰田开展网络安全检查
物体颜色的来源
Jina 实例秀|基于神经搜索的网络安全威胁检测(一)
MySQL:面试问的范式设计
Digital management insight into retail and e-commerce operations - retail password
双向带头循环链表实现
bitset的基本用法
helm安装
RAID介绍及RAID5配置实例
Jenkins使用手册(1) —— 软件安装
Maple 2022 software installation package download and installation tutorial
航企纠缠A350安全问题 空客主动取消飞机订单
Oracle中对临时表空间执行shrink操作