当前位置:网站首页>Force deduction solution summary 450- delete nodes in the binary search tree
Force deduction solution summary 450- delete nodes in the binary search tree
2022-06-12 02:09:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
Given the root node of a binary search tree root And a value key, Delete the key Corresponding node , And keep the property of binary search tree unchanged . Back to binary search tree ( May be updated ) Reference to the root node of .
Generally speaking , Deleting a node can be divided into two steps :
First find the node to be deleted ;
If you find it , Delete it .
Example 1:
Input :root = [5,3,6,2,4,null,7], key = 3
Output :[5,4,6,2,null,null,7]
explain : Given the node value to be deleted is 3, So first we found 3 This node , Then delete it .
The right answer is [5,4,6,2,null,null,7], As shown in the figure below .
The other correct answer is [5,2,6,null,4,null,7].
Example 2:
Input : root = [5,3,6,2,4,null,7], key = 0
Output : [5,3,6,2,4,null,7]
explain : Binary tree does not contain a value of 0 The node of
Example 3:
Input : root = [], key = 0
Output : []
Tips :
Range of nodes [0, 104].
-105 <= Node.val <= 105
Unique node value
root Is a legitimate binary search tree
-105 <= key <= 105
Advanced : The time complexity of the algorithm is required to be O(h),h Is the height of the tree .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/delete-node-in-a-bst
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * First of all, the simplest way must be to convert a binary tree into a queue , Then reorder it . This must be the case , But the time complexity is high , Nor is it the point of this topic . * First, according to the properties of binary tree , find key Corresponding node , Then find the node , The leftmost node of the child nodes on the right replaces the current node . Then find the target node , We still have to parent Get rid of references to it .
Code :
public class Solution450 {
public TreeNode deleteNode(TreeNode root, int key) {
TreeNode recursion = recursion(root, key);
return recursion;
}
private TreeNode recursion(TreeNode current, int key) {
if (current == null) {
return null;
}
if (current.val == key) {
if (current.right != null) {
TreeNode left = current.left;
TreeNode right = current.right;
TreeNode node = findNode(current, current.right);
node.left = left;
if (node != right) {
node.right = right;
}
return node;
}
return current.left;
}
if (current.val > key) {
current.left = recursion(current.left, key);
} else {
current.right = recursion(current.right, key);
}
return current;
}
private TreeNode findNode(TreeNode parent, TreeNode treeNode) {
if (treeNode.left != null) {
return findNode(treeNode, treeNode.left);
}
if (treeNode.right != null) {
parent.left = treeNode.right;
} else {
parent.left = null;
}
return treeNode;
}
}边栏推荐
- xcall 集群脚本(查看jps命令)
- 力扣解法汇总732-我的日程安排表 III
- Graphical data analysis | business analysis and data mining
- Linux (centos7) installer mysql - 5.7
- How to improve the advertising rating of advertising, that is, the quality score?
- 力扣解法汇总699-掉落的方块
- Force deduction solution summary 668- the smallest number k in the multiplication table
- Force deduction solution summary 462- minimum number of moves to make array elements equal II
- 力扣解法汇总398-随机数索引
- ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法
猜你喜欢

DDD的分层架构

商城开发知识点

Add sequence number column to MySQL query result set

Graphical data analysis | business analysis and data mining

matplotlib. pyplot. Bar chart (II)

Oracle 11g graphic download installation tutorial (step by step)

BaseDexClassLoader那些事

程序员应该如何解决买菜难问题?手把手带你利用无障碍辅助功能快速下单抢菜

Does the virtual host have independent IP

Alicloud OSS file upload system
随机推荐
Linux (centos7) installer mysql - 5.7
Force deduction solution summary 396 rotation function
Swiftyjson analyse les fichiers json locaux
ACL 2022 | 预训练语言模型和图文模型的强强联合
Force deduction solution summary 462- minimum number of moves to make array elements equal II
What insurance products can you buy at the age of 60?
Force deduction solution summary 449 serialization and deserialization binary search tree
The establishment and introduction of the announcement module of PHP development blog system
leetcode:6. Zigzag transformation
Three main factors determining advertising quality
How to improve the advertising rating of advertising, that is, the quality score?
Graphic data analysis | business cognition and data exploration
Modification of system module information of PHP security development 12 blog system
What are the preparations for setting up Google search advertising series?
serialization and deserialization
What is SAP c/4hana Foundation
[learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)
力扣解法汇总944-删列造序
Force deduction solution summary 398 random number index
Force deduction solution summary 388- longest absolute path of file