当前位置:网站首页>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);
}
}
}
边栏推荐
- 金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
- SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
- 顶级域名有哪些?是如何分类的?
- 【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
- <No. 8> 1816. Truncate sentences (simple)
- Review and arrangement of HCIA
- Simple network configuration for equipment management
- Hi3516 full system type burning tutorial
- 关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
- What is an esp/msr partition and how to create an esp/msr partition
猜你喜欢
<No. 8> 1816. 截断句子 (简单)
[deep learning] image multi label classification task, Baidu paddleclas
File upload vulnerability - upload labs (1~2)
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
静态Vxlan 配置
OSPF exercise Report
Tutorial on the principle and application of database system (011) -- relational database
On valuation model (II): PE index II - PE band
消息队列消息丢失和消息重复发送的处理策略
随机推荐
利用棧來實現二進制轉化為十進制
Error in compiling libssl
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
Problem: the string and characters are typed successively, and the results conflict
如何理解服装产业链及供应链
Solutions to cross domain problems
Static comprehensive experiment
Tutorial on the principle and application of database system (008) -- exercises on database related concepts
Vxlan static centralized gateway
Vxlan 静态集中网关
gcc 编译报错
BGP third experiment report
Static routing assignment of network reachable and telent connections
消息队列消息丢失和消息重复发送的处理策略
Zero shot, one shot and few shot
Will the filing free server affect the ranking and weight of the website?
idm服务器响应显示您没有权限下载解决教程
Niuke website