当前位置:网站首页>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);
}
}
}
边栏推荐
- [pytorch practice] image description -- let neural network read pictures and tell stories
- <No. 9> 1805. Number of different integers in the string (simple)
- What are the technical differences in source code anti disclosure
- Is it safe to open an account in Ping An Securities mobile bank?
- Zhimei creative website exercise
- Static vxlan configuration
- Is it safe to open Huatai's account in kainiu in 2022?
- 千人规模互联网公司研发效能成功之路
- Using stack to convert binary to decimal
- 即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
猜你喜欢
(to be deleted later) yyds, paid academic resources, please keep a low profile!
【统计学习方法】学习笔记——提升方法
Inverted index of ES underlying principle
Vxlan static centralized gateway
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
Upgrade from a tool to a solution, and the new site with praise points to new value
"Series after reading" my God! It's so simple to understand throttling and anti shake~
Attack and defense world ----- summary of web knowledge points
EPP+DIS学习之路(2)——Blink!闪烁!
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
随机推荐
Minimalist movie website
The road to success in R & D efficiency of 1000 person Internet companies
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Static routing assignment of network reachable and telent connections
EPP+DIS学习之路(2)——Blink!闪烁!
Attack and defense world - PWN learning notes
What are the top-level domain names? How is it classified?
Error in compiling libssl
Tutorial on the principle and application of database system (008) -- exercises on database related concepts
@Bean与@Component用在同一个类上,会怎么样?
@What happens if bean and @component are used on the same class?
Epp+dis learning path (1) -- Hello world!
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
防红域名生成的3种方法介绍
Is it safe to open Huatai's account in kainiu in 2022?
Inverted index of ES underlying principle
Static vxlan configuration
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
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)