发布于2022-01-06 08:18 阅读(1306) 评论(0) 点赞(10) 收藏(4)
大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞与关注是我的最大动力,如有错误还请不吝赐教,万分感谢。一起支持原创吧!纯手打有笔误还望谅解。
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
Search for a node to remove.
If the node is found, delete the node.
Example 1:
Input: root = [5,3,6,2,4,null,7], key = 3
Output: [5,4,6,2,null,null,7]
Explanation: Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
Example 2:
Input: root = [5,3,6,2,4,null,7], key = 0
Output: [5,3,6,2,4,null,7]
Explanation: The tree does not contain a node with value = 0.
Example 3:
Input: root = [], key = 0
Output: []
Constraints:
The number of nodes in the tree is in the range [0, 104].
-105 <= Node.val <= 105
Each node has a unique value.
root is a valid binary search tree.
-105 <= key <= 105
Follow up: Could you solve it with time complexity O(height of tree)?
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/delete-node-in-a-bst
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
☘️首先,先把流程搞清楚,删除二叉树上的节点大概有这几种情况
☘️知道了整个流程,让我们来写代码。首先先写,如何找到左孩子的最右侧节点,找到右孩子的最左侧节点。
public TreeNode findLeftRightest(TreeNode root) {
root = root.left;
while (root.right != null) {
root = root.right;
}
return root;
}
public TreeNode findRightLeftest(TreeNode root) {
root = root.right;
while (root.left != null) {
root = root.left;
}
return root;
}
☘️接着如果你做过Java描述 LeetCode,701. Insert into a Binary Search Tree 在二叉搜索树中插入一个值,这道题,就可以把框架先打起来了。
if (root == null) {
return null;
}
if (key < root.val) {
root.left = deleteNode(root.left, key);
}
if (key > root.val) {
root.right = deleteNode(root.right, key);
}
if (key == root.val) {
}
return root;
☘️关键就是命中的时候,由上面的701题可以知道,我们函数返回的都是重新构建之后的那个节点,所以根据上面的几种情况我们来看。
if (root.left == null && root.right == null) {
return null;
}
if (root.left != null && root.right == null) {
TreeNode leftRightest = findLeftRightest(root);
root.val = leftRightest.val;
root.left = deleteNode(root.left, leftRightest.val);
}
首先找到最左侧的最右节点leftRightest,之后,将root的值替换成它,之后想办法把leftRightest这个节点删除不就OK了?所以继续递归,进行删除。if ((root.right != null && root.left == null)
|| (root.right != null && root.left != null)) {
TreeNode rightLeftest = findRightLeftest(root);
root.val = rightLeftest.val;
root.right = deleteNode(root.right, rightLeftest.val);
}
☘️最终的代码如下:
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) {
return null;
}
if (key < root.val) {
root.left = deleteNode(root.left, key);
}
if (key > root.val) {
root.right = deleteNode(root.right, key);
}
if (key == root.val) {
if (root.left == null && root.right == null) {
return null;
}
if (root.left != null && root.right == null) {
TreeNode leftRightest = findLeftRightest(root);
root.val = leftRightest.val;
root.left = deleteNode(root.left, leftRightest.val);
}
if ((root.right != null && root.left == null)
|| (root.right != null && root.left != null)) {
TreeNode rightLeftest = findRightLeftest(root);
root.val = rightLeftest.val;
root.right = deleteNode(root.right, rightLeftest.val);
}
}
return root;
}
public TreeNode findLeftRightest(TreeNode root) {
root = root.left;
while (root.right != null) {
root = root.right;
}
return root;
}
public TreeNode findRightLeftest(TreeNode root) {
root = root.right;
while (root.left != null) {
root = root.left;
}
return root;
}
主要就是把二叉搜索树的删除操作再复习了一遍。
原文链接:https://blog.csdn.net/qq_41376740/article/details/122318702
作者:程序员之神
链接:http://www.javaheidong.com/blog/article/372902/b27e9c9f4650fbaccbd3/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!