代码随想录第十九天(一刷&&C语言)|二叉搜索树的最近公共祖先&&二叉搜索树中的插入操作&&删除二叉搜索树中的节点

创作目的:为了方便自己后续复习重点,以及养成写博客的习惯。

一、二叉搜索树的最近公共祖先

思路:参考了ledcode题解和carl的文档,二叉搜索树符合中序遍历,不用使用回溯,可以方便的从上向下查找目标区间,遇到目标区间内的节点,直接返回。

ledcode题目:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/description/

代码随想录第十九天(一刷&&C语言)|二叉搜索树的最近公共祖先&&二叉搜索树中的插入操作&&删除二叉搜索树中的节点_第1张图片

AC代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode** getPath(struct TreeNode* root,struct TreeNode* target,int*num)
{
    struct TreeNode** path = malloc(sizeof(struct TreeNode*)*2001);
    struct TreeNode* node = root;
    while(node != target) {
        path[(*num)++] = node;  //把与target不等的节点同path存起来;
        if(target->val < node->val) {
            node = node->left;
        }else {
            node = node->right;
        }
    }
    path[(*num)++] = node;
    return path;
}
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    int num_p = 0,num_q = 0;
    struct TreeNode** path_p = getPath(root,p,&num_p);
    struct TreeNode** path_q = getPath(root,q,&num_q);
    struct TreeNode* ancestor;
    for(int i = 0;i < num_p && i < num_q; ++i ) {
        if(path_p[i] == path_q[i]) {
            ancestor = path_p[i];
        }else {
            break;
        }
    }
    return ancestor;
}

二、二叉搜索树中的插入操作

思路:利用搜索树的中序遍历特性,使用cur节点为循环条件,去寻找cur->left与cur->right为空的地方,创建新节点并赋val。

lecode题目:https://leetcode.cn/problems/insert-into-a-binary-search-tree/description/

代码随想录第十九天(一刷&&C语言)|二叉搜索树的最近公共祖先&&二叉搜索树中的插入操作&&删除二叉搜索树中的节点_第2张图片

AC代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 struct TreeNode* createTreeNode(int val) 
 {
     struct TreeNode* ret = malloc(sizeof(struct TreeNode));
     ret->val = val;
     ret->left = ret->right = NULL;
     return ret;
 }
struct TreeNode* insertIntoBST(struct TreeNode* root, int val) 
{
    if(root == NULL) {
        root = createTreeNode(val);
        return root;
    }   
    struct TreeNode* cur = root;
    while(cur != NULL) {
        if(val < cur->val) {
            if(cur->left == NULL) {
                cur->left = createTreeNode(val);
                break;
            }else {
                cur = cur->left;
            }
        }else {
            if(cur->right == NULL) {
                cur->right = createTreeNode(val);
                break;
            }else {
                cur = cur->right;
            }
        }
    }
    return root;
}

三、删除二叉搜索树中的节点

思路:比较给定值与节点值的大小,不相等则递归。

ledcode题目:https://leetcode.cn/problems/delete-node-in-a-bst/description/

代码随想录第十九天(一刷&&C语言)|二叉搜索树的最近公共祖先&&二叉搜索树中的插入操作&&删除二叉搜索树中的节点_第3张图片

AC代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


struct TreeNode* deleteNode(struct TreeNode* root, int key){
    if(root == NULL) {
        return NULL;
    }
    if(root->val > key) {
        root->left = deleteNode(root->left,key);
        return root;
    }
    if(root->val < key) {
        root->right = deleteNode(root->right,key);
        return root;
    }
    if(root->val == key) {
        if(!root->left && !root->right) {
            return NULL;
        }
        if(!root->right) {
            return root->left;
        }
        if(!root->left) {
            return root->right;
        }
        struct TreeNode*successor = root->right;
        while(successor->left) {
            successor = successor->left;
        }
        root->right = deleteNode(root->right,successor->val);
        successor->right = root->right;
        successor->left  = root->left;
        return successor;
    }
    return root;
}

全篇后记:

        通过二叉搜索树的公共祖先,复习了一下二维指针解题的便利。解题留意一下给定值的比较,坚持刷下去,收获会更多。

你可能感兴趣的:(Carl代码随想录练习记录,c语言,开发语言)