当前位置:网站首页>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
- EPP+DIS学习之路(1)——Hello world!
- 111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
- Inverted index of ES underlying principle
- 【统计学习方法】学习笔记——第四章:朴素贝叶斯法
- Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
- Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
- Tutorial on the principle and application of database system (011) -- relational database
猜你喜欢

The left-hand side of an assignment expression may not be an optional property access. ts(2779)

普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备

【统计学习方法】学习笔记——支持向量机(上)

About web content security policy directive some test cases specified through meta elements

RHSA first day operation

(to be deleted later) yyds, paid academic resources, please keep a low profile!

Several methods of checking JS to judge empty objects

Introduction and application of smoothstep in unity: optimization of dissolution effect

Tutorial on principles and applications of database system (009) -- conceptual model and data model

即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
随机推荐
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Upgrade from a tool to a solution, and the new site with praise points to new value
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
千人规模互联网公司研发效能成功之路
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
利用棧來實現二進制轉化為十進制
[deep learning] image multi label classification task, Baidu paddleclas
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
让数字管理好库存
"Series after reading" my God! It's so simple to understand throttling and anti shake~
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
Niuke website
(待会删)yyds,付费搞来的学术资源,请低调使用!
Epp+dis learning path (1) -- Hello world!
Is it safe to open Huatai's account in kainiu in 2022?
Routing strategy of multi-point republication [Huawei]
SQL Lab (46~53) (continuous update later) order by injection
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
zero-shot, one-shot和few-shot
@Bean与@Component用在同一个类上,会怎么样?