代码随想录day21| 530.二叉搜索树的最小绝对差 、501.二叉搜索树中的众数 、 236. 二叉树的最近公共祖先

530. 二叉搜索树的最小绝对差


int countNodes(struct TreeNode* root) {  
    if (!root) return 0;  
    return 1 + countNodes(root->left) + countNodes(root->right);  
}  
  

void fillArray(struct TreeNode* root, int* nums, int* index) {  
    if (!root) return;  
    fillArray(root->left, nums, index);  
    nums[(*index)++] = root->val;  
    fillArray(root->right, nums, index);  
}  
  
int getMinimumDifference(struct TreeNode* root) {  
    if (!root) return 0;
      
    int nodeCount = countNodes(root);  
    int* nums = (int*)malloc(nodeCount * sizeof(int));  
   
    int index = 0;  
    fillArray(root, nums, &index);  
      
    int minDiff = INT_MAX;  
    for (int i = 1; i < nodeCount; i++) {  
        int diff = nums[i] - nums[i - 1];  
        if (diff < minDiff) {  
            minDiff = diff;  
        }  
    }   
    return minDiff;  
}

因为二叉搜查树是一个有序的,所以可以将存入一个数组里面,然后我们不断去对比

双指针

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

void dfs (struct TreeNode* root, int* pre, int* ans){
    if(!root)
        return;
     dfs(root->left, pre, ans);
    
    if(*pre == -1){
        *pre = root->val;
    }
    else
 {
    *ans = fmin(*ans, root->val-(*pre));
    *pre = root->val;
 }

    dfs(root->right, pre, ans);
        
}
int getMinimumDifference(struct TreeNode* root) {
    int ans = INT_MAX, pre = -1;
    dfs(root, &pre, &ans);
    return ans;
    
}

501. 二叉搜索树中的众数

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* answer;
int answerSize;
int base, count , maxCount;

void update(int x){
    if( x == base){
        ++count;
    }
    else{
        count = 1;
        base = x;
    }
    if(count == maxCount){
        answer[answerSize++] =  base;

    }
    if(count > maxCount){
        maxCount = count;
        answerSize = 0;
        answer[answerSize++] = base;
    }
}
void dfs(struct TreeNode* node){
    if(!node)
     return;
    dfs(node->left);
    update(node->val);
    dfs(node->right);
}
int* findMode(struct TreeNode* root, int* returnSize) {
    base = count = maxCount = 0;
    answer = malloc(sizeof(int)*4001);
    answerSize  = 0;
    dfs(root);
    *returnSize = answerSize;
    return answer;
}

236. 二叉树的最近公共祖先

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    if(!root)
        return NULL;
    if(root == p || root == q){
        return root;
    }
    struct TreeNode* left = lowestCommonAncestor(root->left, p, q);
    struct TreeNode* right = lowestCommonAncestor(root->right, p, q);
    
    if(left && right)
        return root;
    if(!left && right){
        return right;
    }
    else if(left && !right)
        return left;
    else
        return NULL;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值