当前位置:网站首页>Notes on brushing questions (19) -- binary tree: modification and construction of binary search tree
Notes on brushing questions (19) -- binary tree: modification and construction of binary search tree
2022-06-26 15:14:00 【Dream of becoming a skinhead!】
Catalog
List of articles
Brush notes ( One )– An array type : Dichotomy
Brush notes ( Two )– An array type : Double finger needling
Brush notes ( 3、 ... and )– An array type : The sliding window
Brush notes ( Four )– An array type : simulation
Brush notes ( 5、 ... and )– List type : Basic topics and operations
Brush notes ( 6、 ... and )– Hashtable : Basic topics and ideas
Brush notes ( 7、 ... and )– character string : Classic title
Brush notes ( 8、 ... and )– Double pointer : Sum of two numbers and extension
Brush notes ( Nine )– character string :KMP Algorithm
Brush notes ( Ten )– Stacks and queues : Basic topics
Brush notes ( 11、 ... and )– Stacks and queues :Top-K problem
Brush notes ( Twelve )– review : Sorting algorithm
Brush notes ( 13、 ... and )– Binary tree : Traversal in front, middle and back order ( review )
Brush notes ( fourteen )– Binary tree : Sequence traversal and DFS,BFS
Brush notes ( 15、 ... and )– Binary tree : Attribute related topics
Brush notes ( sixteen )– Binary tree : Modification and construction
Brush notes ( seventeen )– Binary search tree : About attributes
Brush notes ( eighteen )– Binary tree : The common ancestor problem
Preface
The last blog of binary tree !!! The second part is the backtracking algorithm !! start doing sth. !
Bibliography
701. Insert operation in binary search tree
The title links are as follows :
701. Insert operation in binary search tree
The screenshot of the title is as follows :

Binary search tree here the problem must pay attention to a very important skill , Is its postorder traversal , Because of the particularity of post order traversal of binary search tree , So most of the time, topics evolve according to post order traversal .
recursive _DFS
public class Insert operation in binary search tree {
public TreeNode insertIntoBST(TreeNode root, int val) {
// If the current node is empty, it will go to the bottom , At this point, you can directly construct a new node
if(root == null) return new TreeNode(val);
// Judge the relationship between the value of the current node and the left and right subtrees , So as to determine the next step
if(root.val > val){
root.left = insertIntoBST(root.left,val);
}else{
root.right = insertIntoBST(root.right,val);
}
return root;
}
}
iteration _BFS
How to say the insertion operation of binary tree , In fact, it is a process of continuous traversal . But in the process of traversal , To save the values of two nodes , That is, the values of the current node and the previous node .
public class Insert operation in binary search tree _ Iterative writing {
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root == null) return new TreeNode(val);
// Define two node pointers , Used to save the current node and the previous node
TreeNode parent = root,key = root;
while(key != null){
parent = key;
key = key.val < val ? key.right : key.left;
}
// Pay attention , Here, if the value of the inserted node is equal to the current node , The value of the current node will be overwritten
if(parent.val > val) parent.left = new TreeNode(val);
else if(parent.val < val) parent.right = new TreeNode(val);
return root;
}
}
450. Delete nodes in binary search tree
The title links are as follows :
450. Delete nodes in binary search tree
The screenshot of the title is as follows :

This topic , It can be roughly divided into the following steps
1. Find the node to be deleted
2. Classify the nodes to be deleted
<1> The node to be deleted is a leaf node
<2> The left subtree of the node to be deleted is empty
<3> The right subtree of the node to be deleted is empty
<4> The left and right subtrees of the node to be deleted are sound
3. Delete the corresponding node
The specific code is as follows :
public class Delete operation in binary search tree {
public TreeNode deleteNode(TreeNode root, int key) {
TreeNode cur = root;// Pointer to the current
TreeNode parent = null;// Record the last traversal node
// Find the node to be deleted
while(cur != null){
if(cur.val > key){
parent = cur;
cur = cur.left;
}else if(cur.val < key){
parent = cur;
cur = cur.right;
}else{
break;
}
}
if(cur == null) return root;// If the node to be deleted is not found , Just return to the root node ( The test cases are also included here [] The situation of )
if(cur.left == null){
//1. The left subtree is empty ( It may be the root node ) At the same time processing
if(parent == null){
// The identification of this situation is added here because the node to be deleted may be the root node
root = cur.right;
}else{
if(parent.left == cur) parent.left = cur.right;
else parent.right = cur.right;
}
cur.right = null;
}
else if(cur.right == null){
//2. The right subtree is empty ( It may be the root node )
if(parent == null){
// The identification of this situation is added here because the node to be deleted may be the root node
root = cur.left;
}else {
if (parent.left == cur) parent.left = cur.left;
else parent.right = cur.left;
}
cur.left = null;
}
else{
// This is the last case , That is, the left and right subtrees of the current node are , This one is special , The deletion here needs a different form
// This deletion is to find a suitable node value to overwrite
parent = cur;
TreeNode fac = cur.right;// This node is used to find the maximum value of the left subtree or the minimum value of the right subtree
// In general, our choice is the minimum value of right subtree
while(fac.left != null){
parent = fac;
fac = fac.left;
}
// Overwrite the current node to be deleted after finding it
cur.val = fac.val;
if(parent.left == fac){
// If the found node is the left child tree of the parent node
parent.left = fac.right;
}else{
// If the found node is the right subtree of the parent node
parent.right = fac.right;
}
}
return root;
}
}
669. Prune the binary search tree
The title links are as follows :
669. Prune the binary search tree
The screenshot of the title is as follows :

How to say the pruning here , Although it is a simple question , But the pruning process is actually very painful .
public class Prune the binary search tree {
public TreeNode trimBST(TreeNode root, int low, int high) {
if(root == null) return null;
// Then there is post order traversal , First, traverse the left and right subtrees
root.left = trimBST(root.left,low,high);
root.right = trimBST(root.right,low,high);
// Then start with the root node of the current tree
// If the root node does not meet the requirements , Then deal with the left and right subtrees
if(root.val < low) return trimBST(root.right,low,high);
if(root.val > high) return trimBST(root.left,low,high);
return root;
}
}
108. Convert an ordered array to a binary search tree
The title links are as follows :
108. Convert an ordered array to a binary search tree
The screenshot of the title is as follows :
public class Turn an ordered array into a binary search tree {
public TreeNode sortedArrayToBST(int[] nums) {
if(nums.length == 0) return null;
return solve(nums,0, nums.length);
}
// It's not very complicated , Because it is no different from building a normal binary tree , Because its value is very fixed , It's always taken from the middle .
public TreeNode solve(int[] nums,int left,int right){
// if left > right Just go back to one null Nodes are fine
if(left >= right) return null;
int mid = left + (right - left) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = solve(nums,left,mid);
root.right = solve(nums,mid + 1,right);
return root;
}
}
538. Convert binary search tree to accumulation tree
The title links are as follows :
538. Convert binary search tree to accumulation tree
The screenshot of the title is as follows :

Although this question is medium , But it's not very difficult . Our middle order traversal is a left subtree >> The root node >> Right subtree , But the traversal method required here is right subtree >> The root node >> The left subtree , So just change the order .
public class Binary search tree is transformed into cumulative tree {
int num;
public TreeNode convertBST(TreeNode root) {
if(root == null) return null;
solve(root);
return root;
}
public void solve(TreeNode root){
if(root == null) return;
solve(root.right);
root.val += num;
num = root.val;
solve(root.left);
}
}
边栏推荐
- 【TcaplusDB知识库】TcaplusDB运维单据介绍
- The intersect function in the dplyr package of R language obtains the data lines that exist in both dataframes and the data lines that cross the two dataframes
- Advanced operation of MySQL database basic SQL statement tutorial
- Comparative analysis of restcloud ETL and kettle
- There are so many vulnerabilities in tcp/ip protocol?
- English grammar_ Adjective / adverb Level 3 - original sentence pattern
- 5 figures illustrate the container network
- Redis transaction and watch instruction
- One click GCC script installation
- 杜老师说网站更新图解
猜你喜欢
![[tcapulusdb knowledge base] Introduction to tcapulusdb general documents](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb general documents

小程序:uniapp解决 vendor.js 体积过大的问题

文献1

View touch analysis

【小程序实战系列】小程序框架 页面注册 生命周期 介绍

【ceph】cephfs caps简介

重磅白皮书发布,华为持续引领未来智慧园区建设新模式

Smoothing data using convolution

乐鑫 AWS IoT ExpressLink 模组达到通用可用性

About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)
随机推荐
Pytoch deep learning code skills
Unity C# 网络学习(十)——UnityWebRequest(二)
Database - integrity constraints
【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
【ceph】CephFS 内部实现(四):MDS是如何启动的?--未消化
RestCloud ETL解决shell脚本参数化
聊聊几位大厂清华同学的近况
Halcon C # sets the form font and adaptively displays pictures
The tablestack function of the epidisplay package of R language makes a statistical summary table (descriptive statistics of groups, hypothesis test, etc.), does not set the by parameter to calculate
vsomeip3 双机通信文件配置
Document 1
cluster addslots建立集群
Unity C# 网络学习(八)——WWW
【ceph】cephfs的锁 笔记
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、使用summary函数查看简单模型的汇总统计信息
Minister of investment of Indonesia: Hon Hai is considering establishing electric bus system and urban Internet of things in its new capital
One click GCC script installation
Principle of TCP reset attack
Pod scheduling of kubernetes
Database - sequence