[Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST

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:

  1. Search for a node to remove.
  2. If the node is found, delete the node.

Note: Time complexity should be O(height of tree).

Example:

root = [5,3,6,2,4,null,7]
key = 3

    5
   /   3   6
 / \   2   4   7

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 following BST.

    5
   /   4   6
 /     2       7

Another valid answer is [5,2,6,null,4,null,7].

    5
   /   2   6
   \       4   7


给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。

一般来说,删除节点可分为两个步骤:

  1. 首先找到需要删除的节点;
  2. 如果找到了,删除它。

说明: 要求算法时间复杂度为 O(h),h 为树的高度。

示例:

root = [5,3,6,2,4,null,7]
key = 3

    5
   /   3   6
 / \   2   4   7

给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。

一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示。

    5
   /   4   6
 /     2       7

另一个正确答案是 [5,2,6,null,4,null,7]。

    5
   /   2   6
   \       4   7

152ms
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func deleteNode(_ root: TreeNode?, _ key: Int) -> TreeNode? {
16         /*
17          *  思路:
18          *  1,找到需要被删除的节点
19          *  2,没有子节点,直接删除
20          *  3,一个子节点,直接替换
21          *  4,两个子节点,中序遍历的第一个节点替换,BST的中序遍历就是排好序的
22          */
23         if root == nil {return root}
24         var temp = root
25         // 在左子树中寻找删除
26         if temp!.val > key{
27
28             temp?.left = deleteNode(temp?.left, key)
29         }else if temp!.val < key{
30             // 在右子树中删除
31             temp?.right = deleteNode(temp?.right, key)
32         }else{
33             // 找到了需要删除的节点,如果节点有一个子节点
34             if temp?.left == nil || temp?.right == nil{
35                 temp = temp?.left == nil ? temp?.right : temp?.left
36             }else{
37                 var node = temp!.right!
38                 while node.left != nil{
39                     node = node.left!
40                 }
41                 temp!.val = node.val
42                 temp?.right = deleteNode(temp?.right, node.val)
43             }
44         }
45         return temp
46     }
47 }


164ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func deleteNode(_ root: TreeNode?, _ key: Int) -> TreeNode? {
16         guard let root = root else {
17             return nil
18         }
19
20         if key < root.val {
21             root.left = deleteNode(root.left, key)
22         } else if key > root.val {
23             root.right = deleteNode(root.right, key)
24         } else {
25             if root.left == nil {
26                 return root.right
27             } else if root.right == nil {
28                 return root.left
29             } else {
30                 let minNode = findMin(root.right!)
31                 root.val = minNode.val
32                 root.right = deleteNode(root.right, root.val)
33             }
34         }
35
36         return root
37     }
38
39     func findMin(_ root: TreeNode) -> TreeNode {
40         var root = root
41
42         while let leftNode = root.left {
43             root = leftNode
44         }
45
46         return root
47     }
48 }


184ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func deleteNode(_ root: TreeNode?, _ key: Int) -> TreeNode? {
16         var root = root
17         if root == nil {return nil}
18         if root!.val == key
19         {
20             if root!.right == nil {return root!.left}
21             else
22             {
23                 var cur:TreeNode? = root!.right
24                 while(cur?.left != nil)
25                 {
26                     cur = cur!.left
27                 }
28                 (root!.val,cur!.val) = (cur!.val,root!.val)
29             }
30         }
31         root!.left = deleteNode(root!.left, key)
32         root!.right = deleteNode(root!.right, key)
33         return root
34     }
35 }

原文地址:https://www.cnblogs.com/strengthen/p/10341750.html

时间: 2024-08-24 23:55:18

[Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST的相关文章

450. 删除二叉搜索树中的节点

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution 11 { 12 public: 13 TreeNode* deleteNode(Tr

给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的&#160;key&#160;对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用

一般来说,删除节点可分为两个步骤: 首先找到需要删除的节点: 如果找到了,删除它. 说明: 要求算法时间复杂度为 O(h),h 为树的高度. 示例: root = [5,3,6,2,4,null,7] key = 3 5 / 3 6 / \ 2 4 7 给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它. 一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示. 5 / 4 6 / 2 7 另一个正确答案是 [5,2,6,null,4,null,7]. 5

二叉搜索树中的常用方法

1 package Tree; 2 3 import org.junit.Test; 4 5 class TreeNode { 6 7 int val = 0; 8 TreeNode left = null; 9 TreeNode right = null; 10 11 public TreeNode(int val) { 12 this.val = val; 13 14 } 15 16 } 17 18 public class BinarySearchTree { 19 20 /** 21 *

leetcode 二叉搜索树中第K小的元素 python

二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [3,1,4,null,2], k = 1 3 / 1 4   2 输出: 1 示例 2: 输入: root = [5,3,6,2,4,null,null,1], k = 3 5 / 3 6 / 2 4 / 1 输出: 3 进阶:如果二叉搜索树经常被修改(插入/删除操作)并且

[LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点

Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in the tree, return null. 给一个二叉搜索树和它的一个节点,找出它的中序后继节点,如果没有返回null. 解法1: 用中序遍历二叉搜索树,当找到root.val = p.val的时

Leetcode 701. 二叉搜索树中的插入操作

题目链接 https://leetcode.com/problems/insert-into-a-binary-search-tree/description/ 题目描述 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树. 返回插入后二叉搜索树的根节点. 保证原始二叉搜索树中不存在新值. 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可. 你可以返回任意有效的结果. 例如, 给定二叉搜索树: 4 / 2 7 / 1 3 和 插入的值: 5 你可以返回这个

230. 二叉搜索树中第K小的元素

230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 解题思路 中序遍历,利用Python3中提供的生成器方法: 中序遍历,判断存储结点值的数组是否到到k,则表明访问的一个结点就是第k个最小的元素: 先获取跟结点处于的位置(第几个最小的元素),如果它比k小,则从右子结点中找,如果它比k大,则从左子节点中找: 实现 class Solution:    

算法dfs——二叉搜索树中最接近的值 II

901. 二叉搜索树中最接近的值 II 中文 English 给定一棵非空二叉搜索树以及一个target值,找到 BST 中最接近给定值的 k 个数. 样例 样例 1: 输入: {1} 0.000000 1 输出: [1] 解释: 二叉树 {1},表示如下的树结构: 1 样例 2: 输入: {3,1,4,#,2} 0.275000 2 输出: [1,2] 解释: 二叉树 {3,1,4,#,2},表示如下的树结构: 3 / 1 4 2 挑战 假设是一棵平衡二叉搜索树,你可以用时间复杂度低于O(n)

[Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod