当前位置:网站首页>Leetcode question brushing: binary tree 26 (insertion operation in binary search tree)
Leetcode question brushing: binary tree 26 (insertion operation in binary search tree)
2022-07-07 12:48:00 【Taotao can't learn English】
701. Insert operation in binary search tree
Given a binary search tree (BST) The root node of and the value to insert into the tree , Insert values into a binary search tree . Return to the root node of the binary search tree after insertion . Input data guarantee , The new value is different from any node value in the original binary search tree .
Be careful , There may be many effective insertion methods , As long as the tree remains a binary search tree after insertion . You can return any valid result .

Tips :
- The number of nodes on a given tree is between 0 and 10^4 Between
- Each node has a unique integer value , Value range from 0 To 10^8
- -10^8 <= val <= 10^8
- The new value is different from any node value in the original binary search tree
A very simple question , Traverse in the way of binary search tree , When you encounter leaf nodes, you can directly judge the size , Add left or right... As appropriate ; Single output node encountered , First judge the size , If it is a branch without degree , Then add a new left or right node directly on this node , If it is a branch with out degree , Loop to that node and then process .
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. Insert operation in binary search tree **/
public class InsertIntoBST {
public TreeNode insertIntoBST(TreeNode root, int val) {
// Empty root node , Just create a node and return
if (root == null) {
return new TreeNode(val);
}
TreeNode node = root;
insert(node, val);
return root;
}
public void insert(TreeNode root, int val) {
// The current node is a leaf node
if (root.left == null && root.right == null) {
if (root.val > val) {
// The current value is larger than the target value
root.left = new TreeNode(val);
} else {
root.right = new TreeNode(val);
}
return;
}
// Single output
if (root.left == null && root.right != null) {
if (root.val > val) {
// The current value is larger than the target value
root.left = new TreeNode(val);
return;
}
}
if (root.left != null && root.right == null) {
if (root.val < val) {
// The current value is smaller than the target value
root.right = new TreeNode(val);
return;
}
}
if (root.val > val) {
// The current value is larger than the target value
insert(root.left, val);
} else {
insert(root.right, val);
}
}
}

边栏推荐
- Attack and defense world ----- summary of web knowledge points
- Guangzhou held work safety conference
- sql-lab (54-65)
- Polymorphism, final, etc
- Connect to blog method, overload, recursion
- About IPSec
- SQL Lab (41~45) (continuous update later)
- Common knowledge of one-dimensional array and two-dimensional array
- 【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
- [pytorch practice] use pytorch to realize image style migration based on neural network
猜你喜欢

File upload vulnerability - upload labs (1~2)

【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移

Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios

Decrypt gd32 MCU product family, how to choose the development board?

解密GD32 MCU产品家族,开发板该怎么选?

leetcode刷题:二叉树21(验证二叉搜索树)
![[pytorch practice] image description -- let neural network read pictures and tell stories](/img/39/b2c61ae0668507f50426b01f2deee4.png)
[pytorch practice] image description -- let neural network read pictures and tell stories
![[statistical learning methods] learning notes - Chapter 5: Decision Tree](/img/0e/c60e04ab4a7ae4728cc76eff1c028a.png)
[statistical learning methods] learning notes - Chapter 5: Decision Tree

What if does not match your user account appears when submitting the code?

leetcode刷题:二叉树23(二叉搜索树中的众数)
随机推荐
Decrypt gd32 MCU product family, how to choose the development board?
Static vxlan configuration
[statistical learning methods] learning notes - Chapter 5: Decision Tree
OSPF exercise Report
【从 0 开始学微服务】【00】课程概述
HZOJ #240. 图形打印四
Zhimei creative website exercise
Vxlan static centralized gateway
【从 0 开始学微服务】【02】从单体应用走向服务化
Realize a simple version of array by yourself from
IPv6 experiment
Session
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
Aike AI frontier promotion (7.7)
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
Guangzhou held work safety conference
静态Vxlan 配置
RHSA first day operation
[statistical learning methods] learning notes - Chapter 4: naive Bayesian method
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)