Day18- 二叉树part07

一、二叉搜索树的最小绝对差

题目一:530. 二叉搜索树的最小绝对差 

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

给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。

差值是一个正数,其数值等于两值之差的绝对值。

inorder 函数递归地进行中序遍历。在访问每个节点时,它计算当前节点和前一个节点的差值,并更新最小差值。这种方法确保了比较的是在BST中排序后相邻的节点,从而找到最小的差值。

/*
 * @lc app=leetcode.cn id=530 lang=cpp
 *
 * [530] 二叉搜索树的最小绝对差
 */

// @lc code=start
/**
 * 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:
    int getMinimumDifference(TreeNode* root) {
        int minDiff = INT_MAX;
        TreeNode* prev = nullptr;
        inorder(root, prev, minDiff);
        return minDiff;
    }

private:
    void inorder(TreeNode* node, TreeNode*& prev, int& minDiff) {
        if (node == nullptr) return;

        inorder(node->left, prev, minDiff);

        if (prev != nullptr) {
            minDiff = std::min(minDiff, node->val - prev->val);
        }
        prev = node;

        inorder(node->right, prev, minDiff);
    }
};
// @lc code=end

 二、二叉搜索树中的众数

题目一:501. 二叉搜索树中的众数

501. 二叉搜索树中的众数

给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。

如果树中有不止一个众数,可以按 任意顺序 返回。

假定 BST 满足如下定义:

  • 结点左子树中所含节点的值 小于等于 当前节点的值
  • 结点右子树中所含节点的值 大于等于 当前节点的值
  • 左子树和右子树都是二叉搜索树

使用中序遍历来访问树中的每个节点。

inorder 函数通过引用传递一个指向上一个节点的指针,以及当前值的计数和最大计数。

如果当前节点的值与上一个节点的值相同,则增加当前计数;

否则,重置当前计数为1。然后,根据当前计数更新众数列表。

如果当前计数大于最大计数,则清空众数列表并添加当前值;

如果当前计数等于最大计数,则将当前值添加到众数列表中。

/*
 * @lc app=leetcode.cn id=501 lang=cpp
 *
 * [501] 二叉搜索树中的众数
 */

// @lc code=start
/**
 * 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:
    vector<int> findMode(TreeNode* root) {
        vector<int> modes;
        int maxCount = 0;
        int currentCount = 0;
        TreeNode* previous = nullptr;

        inorder(root, previous, currentCount, maxCount, modes);

        return modes;
    }

private:
    void inorder(TreeNode* node, TreeNode*& previous, int& currentCount, int& maxCount, vector<int>& modes) {
        if (node == nullptr) return;

        inorder(node->left, previous, currentCount, maxCount, modes);

        if (previous != nullptr && node->val == previous->val) {
            currentCount++;
        } else {
            currentCount = 1;
        }

        if (currentCount > maxCount) {
            maxCount = currentCount;
            modes.clear();
            modes.push_back(node->val);
        } else if (currentCount == maxCount) {
            modes.push_back(node->val);
        }

        previous = node;

        inorder(node->right, previous, currentCount, maxCount, modes);
    }
};
// @lc code=end

三、二叉树的最近公共祖先

题目一:二叉树的最近公共祖先 

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

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

函数 lowestCommonAncestor 会递归地在二叉树中查找节点 pq

如果当前节点是 pq 或者 nullptr,函数就返回当前节点

然后,它递归地在左右子树中查找 pq

如果 pq 分别在当前节点的左右子树中,那么当前节点就是它们的最近公共祖先

如果 pq 都在左子树或都在右子树中,那么相应子树的返回值就是它们的最近公共祖先

/*
 * @lc app=leetcode.cn id=236 lang=cpp
 *
 * [236] 二叉树的最近公共祖先
 */

// @lc code=start
/**
 * 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 || root == p || root == q) return root;

        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);

        if (left && right) return root;

        return left ? left : right;
    }
};
// @lc code=end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值