当前位置:网站首页>leetcode刷题:二叉树26(二叉搜索树中的插入操作)
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
2022-07-07 10:30:00 【涛涛英语学不进去】
701.二叉搜索树中的插入操作
给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据保证,新值和原始二叉搜索树中的任意节点值都不同。
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。
提示:
- 给定的树上的节点数介于 0 和 10^4 之间
- 每个节点都有一个唯一整数值,取值范围从 0 到 10^8
- -10^8 <= val <= 10^8
- 新值和原始二叉搜索树中的任意节点值都不同
很简单的一题,按二叉搜索树的方式遍历,遇到叶子结点就直接判断大小,根据情况添加左或右;遇到单出度结点,先判断大小,如果是无出度的分支,则直接在本节点上添加新左或右结点,如果是有出度的分支,循环到那个结点再处理。
package com.programmercarl.tree;
/** * @ClassName InsertIntoBST * @Descriotion TODO * @Author nitaotao * @Date 2022/7/6 14:47 * @Version 1.0 * https://leetcode.cn/problems/insert-into-a-binary-search-tree/ * 701. 二叉搜索树中的插入操作 **/
public class InsertIntoBST {
public TreeNode insertIntoBST(TreeNode root, int val) {
//空根结点,就创建一个结点返回
if (root == null) {
return new TreeNode(val);
}
TreeNode node = root;
insert(node, val);
return root;
}
public void insert(TreeNode root, int val) {
//当前结点是叶子结点
if (root.left == null && root.right == null) {
if (root.val > val) {
//当前值比目标值大
root.left = new TreeNode(val);
} else {
root.right = new TreeNode(val);
}
return;
}
//单出度情况
if (root.left == null && root.right != null) {
if (root.val > val) {
//当前值比目标值大
root.left = new TreeNode(val);
return;
}
}
if (root.left != null && root.right == null) {
if (root.val < val) {
//当前值比目标值小
root.right = new TreeNode(val);
return;
}
}
if (root.val > val) {
//当前值比目标值大
insert(root.left, val);
} else {
insert(root.right, val);
}
}
}
边栏推荐
- Tutorial on the principle and application of database system (011) -- relational database
- Hi3516全系统类型烧录教程
- 牛客网刷题网址
- What is an esp/msr partition and how to create an esp/msr partition
- 普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
- "Series after reading" my God! It's so simple to understand throttling and anti shake~
- What is a LAN domain name? How to parse?
- 111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
- @Bean与@Component用在同一个类上,会怎么样?
- 解密GD32 MCU产品家族,开发板该怎么选?
猜你喜欢
What is an esp/msr partition and how to create an esp/msr partition
数据库系统原理与应用教程(007)—— 数据库相关概念
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
30. Feed shot named entity recognition with self describing networks reading notes
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
2022 年第八届“认证杯”中国高校风险管理与控制能力挑战赛
zero-shot, one-shot和few-shot
Review and arrangement of HCIA
随机推荐
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
What are the top-level domain names? How is it classified?
EPP+DIS学习之路(2)——Blink!闪烁!
Hi3516 full system type burning tutorial
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
Typescript interface inheritance
Cenos openssh upgrade to version 8.4
利用棧來實現二進制轉化為十進制
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
Review and arrangement of HCIA
VSCode的学习使用
小红书微服务框架及治理等云原生业务架构演进案例
2022 8th "certification Cup" China University risk management and control ability challenge
(to be deleted later) yyds, paid academic resources, please keep a low profile!
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
What is a LAN domain name? How to parse?
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
gcc 编译报错