当前位置:网站首页>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);
}
}
}
边栏推荐
- HZOJ #236. 递归实现组合型枚举
- 数据库安全的重要性
- opencv的四个函数
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- 2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
- 2022-07-07日报:GAN发明者Ian Goodfellow正式加入DeepMind
- ps链接图层的使用方法和快捷键,ps图层链接怎么做的
- 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)
- What is an esp/msr partition and how to create an esp/msr partition
- 【统计学习方法】学习笔记——第五章:决策树
猜你喜欢
解密GD32 MCU产品家族,开发板该怎么选?
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
[pytorch practice] image description -- let neural network read pictures and tell stories
On valuation model (II): PE index II - PE band
Several ways to clear floating
[deep learning] image multi label classification task, Baidu paddleclas
Decrypt gd32 MCU product family, how to choose the development board?
How to apply @transactional transaction annotation to perfection?
Charles: four ways to modify the input parameters or return results of the interface
随机推荐
MPLS experiment
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
Using stack to convert binary to decimal
Polymorphism, final, etc
Day-15 common APIs and exception mechanisms
IPv6 experiment
Day-19 IO stream
用mysql查询某字段是否有索引
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)
Simple implementation of call, bind and apply
[statistical learning methods] learning notes - improvement methods
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
SQL Lab (46~53) (continuous update later) order by injection
数据库安全的重要性
Cookie
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Object. Simple implementation of assign()
[learn micro services from 0] [02] move from single application to service
Master formula. (used to calculate the time complexity of recursion.)
OSPF exercise Report