java
package com.zhu.algorithms.leetcode.tree;
import com.zhu.algorithms.leetcode.base.TreeNode;
public class DeleteNodeInBST {
public static void main(String[] args) {
int[] preorder = {5, 3, 2, 4, 6, 7};
int[] inorder = {2, 3, 4, 5, 6, 7};
BuildBinaryTree build = new BuildBinaryTree();
TreeNode tree = build.buildTreeByPreIn(preorder, inorder);
DeleteNodeInBST test = new DeleteNodeInBST();
TreeNode newTree = test.deleteNode(tree, 3);
TreePrint treePrint = new TreePrint();
System.out.println("the preorder of tree after delete");
treePrint.preOrderPrint(newTree);
System.out.println();
System.out.println("the inorder of tree after delete");
treePrint.inOrderPrint(newTree);
}
public TreeNode getMin(TreeNode root) {
TreeNode node = root;
while (node.left != null) {
node = node.left;
}
return node;
}
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) {
return null;
}
if (root.val == key) {
if (root.left == null) {
return root.right;
}
if (root.right == null) {
return root.left;
}
TreeNode minNode = getMin(root.right);
root.right = deleteNode(root.right, minNode.val);
minNode.left = root.left;
minNode.right = root.right;
root = minNode;
} else if (root.val > key) {
root.left = deleteNode(root.left, key);
} else if (root.val < key) {
root.right = deleteNode(root.right, key);
}
return root;
}
}
go
package tree
func deleteNode(root *TreeNode, key int) *TreeNode {
if root == nil {
return nil
}
if root.Val == key {
if root.Left == nil {
return root.Right
}
if root.Right == nil {
return root.Left
}
minNode := getMinNode(root.Right)
root.Right = deleteNode(root.Right, minNode.Val)
minNode.Left = root.Left
minNode.Right = root.Right
root = minNode
} else if root.Val > key {
root.Left = deleteNode(root.Left, key)
} else if root.Val < key {
root.Right = deleteNode(root.Right, key)
}
return root
}
func getMinNode(root *TreeNode) *TreeNode {
node := root
for node.Left != nil {
node = node.Left
}
return node
}
c++
#include <iostream>
#include "build_tree.cpp"
using namespace std;
class DeleteNodeInBST {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if (root == nullptr) {
return nullptr;
}
if (root->val == key) {
if (root->left == nullptr) {
return root->right;
}
if (root->right == nullptr) {
return root->left;
}
TreeNode* minNode = getMinNode(root->right);
root->right = deleteNode(root->right, minNode->val);
minNode->left = root->left;
minNode->right = root->right;
root = minNode;
} else if (root->val > key) {
root->left = deleteNode(root->left, key);
} else if (root->val < key) {
root->right = deleteNode(root->right, key);
}
return root;
}
TreeNode* getMinNode(TreeNode* root) {
TreeNode* node = root;
while (node->left != nullptr) {
node = node->left;
}
return node;
}
};
int main() {
vector<int> preorder = {5, 3, 2, 4, 6, 7};
vector<int> inorder = {2, 3, 4, 5, 6, 7};
BuildTree build;
TreeNode* root = build.buildTreeFromPreAndIn(preorder, inorder);
DeleteNodeInBST test;
TreeNode* deleteTree = test.deleteNode(root, 3);
DisplayTree displayTree;
displayTree.displayTreeFromIn(deleteTree);
}
python
from typing import Optional
from data_algo.algo.tree.TreeNode import TreeNode
class DeleteNodeInBST:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if root is None:
return None
if root.val == key:
if root.left is None:
return root.right
if root.right is None:
return root.left
minNode = self.getMinNode(root.right)
root.right = self.deleteNode(root.right, minNode.val)
minNode.left = root.left
minNode.right = root.right
root = minNode
elif root.val > key:
root.left = self.deleteNode(root.left, key)
elif root.val < key:
root.right = self.deleteNode(root.right, key)
return root
def getMinNode(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
node = root
while node.left is not None:
node = node.left
return node