代码随想录算法训练营第22天 | LeetCode235. 二叉搜索树的最近公共祖先,701.二叉搜索树中的插入操作,450.删除二叉搜索树中的节点

@代码随想录算法训练营第22天 | LeetCode235. 二叉搜索树的最近公共祖先,701.二叉搜索树中的插入操作,450.删除二叉搜索树中的节点

235. 二叉搜索树的最近公共祖先

第一遍读题思考

比普通二叉树简单太多,只需要判断root的值是否在p和q的中间还是两边就行。

代码随想录解法思路

一样。

c++代码具体实现注意事项

(递归版本)
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root->val>=min(p->val, q->val) && root->val<=max(p->val, q->val)){
            return root;
        }
        if(root==NULL){
            return NULL;
        }
        if(min(p->val,q->val)>root->val){
            return lowestCommonAncestor(root->right, p, q);
        }
        else{
            return lowestCommonAncestor(root->left, p, q);
        }
    }
};

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

第一遍读题思考

终止条件是如果root为空则返回新建的节点,然后单层逻辑是,如果当前节点的值比val小,就是往右侧插入,所以递归右子树。左子树同理。

代码随想录解法思路

可以使用void traversal的写法,但是注意这里就需要一个parent节点记录上一个节点,不然你新建的node不会链接到原来的树上。挺有用的。

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 {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root==NULL) return new TreeNode(val);

        if(root->val>val){
            root->left=insertIntoBST(root->left, val);
        }

        if(root->val<val){
            root->right=insertIntoBST(root->right, val);
        }

        return root;
    }
};
(递归版本,但是代码随想录)
class Solution {
private:
    TreeNode* parent;
    void traversal(TreeNode* cur, int val) {
        if (cur == NULL) {
            TreeNode* node = new TreeNode(val);
            if (val > parent->val) parent->right = node;
            else parent->left = node;
            return;
        }
        parent = cur;
        if (cur->val > val) traversal(cur->left, val);
        if (cur->val < val) traversal(cur->right, val);
        return;
    }

public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        parent = new TreeNode(0);
        if (root == NULL) {
            root = new TreeNode(val);
        }
        traversal(root, val);
        return root;
    }
};

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

第一遍读题思考

重点是删除之后要怎么把后续的节点接上。假如现在遍历到了要删除的节点,就进行删除操作,删除操作有两种情况:

  1. 右孩子补位,左孩子放在右孩子的左下角。
  2. 左孩子补位,右孩子放在左孩子的右下角。

代码随想录解法思路

直接在主函数中进行递归。终止条件就是root为NULL则返回NULL,说明该节点下的BST没有要删除的东西。
单层逻辑就是考虑所有情况,也就是在root不为NULL的情况线,root的value可能等于key,然后root是否是叶子节点,或者左右孩子受否为空。若root不等于key则递归查找root的左右孩子里有没有要删除的。
记住,这里root一直扮演者要被删除的角色。

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 {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(root==NULL) return root;

        if(root->val==key && root->left==NULL && root->right==NULL){
            return NULL;
        }
        else if(root->val==key && root->left==NULL && root->right!=NULL){
            return root->right;
        }
        else if(root->val==key && root->left!=NULL && root->right==NULL){
            return root->left;
        }
        else if(root->val==key && root->left!=NULL && root->right!=NULL){
            TreeNode* cur = root->left;
            while(cur->right){
                cur = cur->right;
            }
            cur->right = root->right;
            return root->left;
        }
        else{
            root->left = deleteNode(root->left,key);
            root->right = deleteNode(root->right, key);
        }

        return root;
    }
};

你可能感兴趣的:(算法,leetcode,c++)