当前位置:网站首页>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);
}
}
}

边栏推荐
- 【深度学习】图像多标签分类任务,百度PaddleClas
- 数据库系统原理与应用教程(007)—— 数据库相关概念
- Several methods of checking JS to judge empty objects
- RHSA first day operation
- SQL blind injection (WEB penetration)
- SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
- Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
- 数据库系统原理与应用教程(011)—— 关系数据库
- Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
- Problem: the string and characters are typed successively, and the results conflict
猜你喜欢

Airserver automatically receives multi screen projection or cross device projection

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

SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)

<No. 9> 1805. Number of different integers in the string (simple)

解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually

"Series after reading" my God! It's so simple to understand throttling and anti shake~

PowerShell cs-utf-16le code goes online
![111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]](/img/2e/da45198bb6fb73749809ba0c4c1fc5.png)
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]

【统计学习方法】学习笔记——第五章:决策树

@What happens if bean and @component are used on the same class?
随机推荐
防红域名生成的3种方法介绍
【统计学习方法】学习笔记——支持向量机(下)
跨域问题解决方案
How much does it cost to develop a small program mall?
数据库系统原理与应用教程(011)—— 关系数据库
BGP third experiment report
@What happens if bean and @component are used on the same class?
[statistical learning methods] learning notes - improvement methods
Routing strategy of multi-point republication [Huawei]
问题:先后键入字符串和字符,结果发生冲突
30. Feed shot named entity recognition with self describing networks reading notes
盘点JS判断空对象的几大方法
NGUI-UILabel
Niuke website
SQL lab 1~10 summary (subsequent continuous update)
Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
About sqli lab less-15 using or instead of and parsing
ps链接图层的使用方法和快捷键,ps图层链接怎么做的