[Java] Leetcode 450. Delete Node in a BST [Binary Search Tree #5]

preview_player
Показать описание
In this video, I'm going to show you how to solve Leetcode 450. Delete Node in a BST which is related to Binary Search Tree.

Here’s a quick rundown of what you’re about to learn:

⭐️ Course Contents ⭐️
⌨️ (0:00) Question
⌨️ (1:32) Solution Explain
⌨️ (5:25) Code

In the end, you’ll have a really good understanding on how to solve Leetcode 450. Delete Node in a BST and questions that are similar to this Binary Search Tree.

#leetcode #programming #softwareengineering #computerscience
Рекомендации по теме
Комментарии
Автор

Hey Eric, thank you so much for the fantastic explanation. It is finally starting to click for me. This tree business is tricky.

AliMalik-ytex
Автор

class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null) return root;

if(root.val < key) {
root.right = deleteNode(root.right, key);
} else if(root.val > key) {
root.left = deleteNode(root.left, key);
} else {// base case
if(root.right == null) {
return root.left;
} else if(root.left == null) {
return root.right;
} else {
//find the min from right subtree
TreeNode cur = root.right;//4
while(cur.left !=null) {
cur = cur.left;// min from right subtree;
}

root.val = cur.val;//4 min;
root.right = deleteNode(root.right, root.val);//subtree deleted node 4;
}
}
return root;
}
}

AshleyYoung-cu
visit shbcf.ru