本文主要是介绍代码随想录算法训练营第二十二天|235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
235. 二叉搜索树的最近公共祖先
- 刷题https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
- 文章讲解https://programmercarl.com/0235.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88.html
- 视频讲解https://www.bilibili.com/video/BV1Zt4y1F7ww/?share_source=copy_web&vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归法):
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/class Solution {//递归法public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if(root == null){return null;}if(root.val > p.val && root.val > q.val){TreeNode left = lowestCommonAncestor(root.left, p, q);if(left != null){return left;}}if(root.val < p.val && root.val < q.val){TreeNode right = lowestCommonAncestor(root.right, p, q);if(right != null){return right;}}return root;}
}
-
题解(迭代法):
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/class Solution {//迭代法public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {while(root != null){if(root.val > p.val && root.val > q.val){root = root.left;}else if(root.val < p.val && root.val < q.val){root = root.right;}else{return root;}}return null;}
}
701.二叉搜索树中的插入操作
- 刷题https://leetcode.cn/problems/insert-into-a-binary-search-tree/description/
- 文章讲解https://programmercarl.com/0701.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E6%8F%92%E5%85%A5%E6%93%8D%E4%BD%9C.html#%E6%80%9D%E8%B7%AF
- 视频讲解https://www.bilibili.com/video/BV1Et4y1c78Y/?share_source=copy_web&vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归法):
/*** 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 {//递归法public TreeNode insertIntoBST(TreeNode root, int val) {//当前结点为空,说明找到了正好的插入位置//因为二叉搜索树插入均在叶子结点if(root == null){return new TreeNode(val);}if(root.val < val){//插入值大于当前结点,向右遍历//同时接收递归的右分支结点root.right = insertIntoBST(root.right, val);}if(root.val > val){//插入值小于当前结点,向左遍历//同时接收递归的左分支结点root.left = insertIntoBST(root.left, val);}//结束,返回新的二叉搜索树根结点return root;}
}
-
题解(迭代法):
/*** 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 {//迭代法public TreeNode insertIntoBST(TreeNode root, int val) {//若传入二叉树为空,则直接返回nullif(root == null){return new TreeNode(val);}//双指针法初始化//root要参与双指针遍历,用newRoot记录二叉树根结点TreeNode newRoot = root;//pre与root做双指针遍历TreeNode pre = root;//若未找到插入位置,则一直进行双指针循环while(root != null){pre = root;if(root.val > val){root = root.left;}else if(root.val < val){root = root.right;}}//root指针遍历到了插入位置//此时root==null,pre为前一个结点的位置if(pre.val > val){//应插入到pre的左分支pre.left = new TreeNode(val);}else{//,比pre.val大,插入到右分支(题中元素不会有重复)pre.right = new TreeNode(val);}//返回添加结点后的二叉树return newRoot;}
}
450.删除二叉搜索树中的节点
- 刷题https://leetcode.cn/problems/delete-node-in-a-bst/description/
- 文章讲解https://programmercarl.com/0450.%E5%88%A0%E9%99%A4%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.html
- 视频讲解https://www.bilibili.com/video/BV1tP41177us/?share_source=copy_web&vd_source=af4853e80f89e28094a5fe1e220d9062
-
难点图解:
(当待删除结点左右分支均不为空时的删除操作)
-
题解(递归法):
/*** 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 {//递归法,通过返回值递归给上一层构建新二叉树public TreeNode deleteNode(TreeNode root, int key) {root = delete(root, key);return root;}public TreeNode delete(TreeNode root, int key){//若传入树为空,则直接返回nullif(root == null){return null;}if(root.val > key){// 若待删值在root左分支,进行左递归//并承接下一层递归的返回值root.left = delete(root.left, key);}else if(root.val < key){//若待删值在root右分支,进行右递归//并承接下一层递归的返回值root.right = delete(root.right, key);}else{//若找到待删结点//情况1、若待删结点左子树为空右子树不为空,//则直接返回当前结点右分支给root的上一层,删掉root,完成处理if(root.left == null && root.right != null){return root.right;}//情况2、若待删结点子右树为空左子树不为空,//则直接返回当前结点左分支给root的上一层,删掉root,完成处理if(root.left != null && root.right == null){return root.left;}//情况3、若待删结点左右子树均为空,则直接返回null给root上一层,完成删除if(root.left == null && root.right == null){return null;}//情况4、若待删结点左右子树均不为空,要保证删除后仍然满足搜索树,需要进一步处理//找到当前root右分支中的最左结点,这个结点就是最接近root的比root大的结点TreeNode temp = root.right;while(temp.left != null){temp = temp.left;}//当前,temp指向了root右分支的最左结点,而temp.left == null//把原root的左分支加到找到的temp结点的右分支上temp.left = root.left;//将原root的右分支的顶到root的位置上,完成删除操作root = root.right;}return root;}
}
这篇关于代码随想录算法训练营第二十二天|235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!