forked from KnowledgeCenterYoutube/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
450_Delete_Node_in_a_BST
132 lines (122 loc) · 4.13 KB
/
450_Delete_Node_in_a_BST
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Leetcode 450: Delete Node in a BST
Detailed video explanation: https://youtu.be/LyHK6_I3aUY
===============================================
C++:
----
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
int inorder_predecessor(TreeNode* root){
root = root->left;
while(root->right) root = root->right;
return root->val;
}
int inorder_successor(TreeNode* root){
root = root->right;
while(root->left) root = root->left;
return root->val;
}
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(!root) return root;
if(key > root->val) root->right = deleteNode(root->right, key);
else if(key < root->val) root->left = deleteNode(root->left, key);
else{
if(!root->left && !root->right) root = nullptr;
else if(root->left){ // find inorder predecessor
root->val = inorder_predecessor(root);
root->left = deleteNode(root->left, root->val);
}
else { // find inorder successor
root->val = inorder_successor(root);
root->right = deleteNode(root->right, root->val);
}
}
return root;
}
};
Java:
-----
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int inorder_predecessor(TreeNode root){
root = root.left;
while(root.right != null) root = root.right;
return root.val;
}
private int inorder_successor(TreeNode root){
root = root.right;
while(root.left != null) root = root.left;
return root.val;
}
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null) return root;
if(key > root.val) root.right = deleteNode(root.right, key);
else if(key < root.val) root.left = deleteNode(root.left, key);
else{
if(root.left == null && root.right == null) root = null;
else if(root.left != null){ // find inorder predecessor
root.val = inorder_predecessor(root);
root.left = deleteNode(root.left, root.val);
}
else { // find inorder successor
root.val = inorder_successor(root);
root.right = deleteNode(root.right, root.val);
}
}
return root;
}
}
Python3:
--------
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
if root == None: return root
def inorder_predecessor(root):
root = root.left
while root.right != None: root = root.right
return root.val
def inorder_successor(root):
root = root.right
while root.left != None: root = root.left
return root.val
if key > root.val: root.right = self.deleteNode(root.right, key)
elif key < root.val: root.left = self.deleteNode(root.left, key)
else:
if root.left == None and root.right == None: root = None
elif root.left != None: # find inorder predecessor
root.val = inorder_predecessor(root)
root.left = self.deleteNode(root.left, root.val)
else: # find inorder successor
root.val = inorder_successor(root)
root.right = self.deleteNode(root.right, root.val)
return root