当前位置:网站首页>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 (008) -- exercises on database related concepts
- zero-shot, one-shot和few-shot
- Static routing assignment of network reachable and telent connections
- What is a LAN domain name? How to parse?
- 【统计学习方法】学习笔记——提升方法
- Decrypt gd32 MCU product family, how to choose the development board?
- The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
- gcc 编译报错
- Hi3516 full system type burning tutorial
- GCC compilation error
猜你喜欢
![[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer](/img/5f/75549fc328d7ac51f8b97eef2c059d.png)
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer

浅谈估值模型 (二): PE指标II——PE Band

OSPF exercise Report

The road to success in R & D efficiency of 1000 person Internet companies

Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment

Up meta - Web3.0 world innovative meta universe financial agreement

About sqli lab less-15 using or instead of and parsing

Sonar:Cognitive Complexity认知复杂度

@Bean与@Component用在同一个类上,会怎么样?

Learning and using vscode
随机推荐
静态Vxlan 配置
源代码防泄密中的技术区别再哪里
数据库系统原理与应用教程(011)—— 关系数据库
<No. 8> 1816. Truncate sentences (simple)
30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
Problem: the string and characters are typed successively, and the results conflict
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
SQL head injection -- injection principle and essence
免备案服务器会影响网站排名和权重吗?
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
Epp+dis learning road (2) -- blink! twinkle!
Customize the web service configuration file
解密GD32 MCU产品家族,开发板该怎么选?
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
【统计学习方法】学习笔记——支持向量机(上)
Error in compiling libssl
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
How to understand the clothing industry chain and supply chain
Tutorial on the principle and application of database system (008) -- exercises on database related concepts