文章链接: 530.二叉搜索树的最小绝对差 501.二叉搜索树中的众数 236. 二叉树的最近公共祖先

视频链接:530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236. 二叉树的最近公共祖先

题目链接: 530.二叉搜索树的最小绝对差 501.二叉搜索树中的众数 236. 二叉树的最近公共祖先


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

思路:

重点:怎么实现“计算二叉搜索树中任意两个数的差的绝对值的最小值”?

1.运用二叉搜索树的性质:左小右大;

2.运用中序遍历(左中右),可看作得到一个有序的数组,所以要求任意两个数的差的绝对值的最小值,只要后一个节点的值减去前一个节点的值就能得到两个数差的绝对值,最后取其最小值就行。

class Solution {
public:
    int result = INT_MAX;
    TreeNode* pre = NULL;
    void traversal(TreeNode* cur){
        if(cur == NULL) return ;

        // 左
        if(cur->left) traversal(cur->left);

        // 中
        if(pre != NULL){
            result = min(result, cur->val - pre->val);
        }
        pre = cur; // 记录前一个节点

        // 右
        if(cur->right) traversal(cur->right);
    }

    int getMinimumDifference(TreeNode* root) {
       traversal(root); 
       return result;
    }
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.


501.二叉搜索树中的众数

思路:

1.运用双指针,若当前指针与前一个指针所指的值相同,则频率加1;

2.不断更新最大的频率,并将最大频率所对应的值放入结果集中;

class Solution {
public:
    int maxCount = 0;
    int count = 0;
    vector<int> result;
    TreeNode* pre = NULL;

    void searchBST(TreeNode* cur){
        if(cur == NULL) return;

        // 左
        if(cur->left) searchBST(cur->left);

        // 中
        if(pre == NULL) { // 第一个节点
            count = 1;
        } else if (pre->val == cur->val) { // 频率加1
            count++;
        } else { // pre 与 cur 不同的话,频率归为1,即出现次数为1
            count = 1;
        }

        pre = cur; // 更新上一个节点

        if(count == maxCount) {
            result.push_back(cur->val);
        }

        if(count > maxCount) {
            maxCount = count; // 更新最大的频率
            result.clear(); // 记得清空
            result.push_back(cur->val); // 放入频率更大的数
        }

        // 右
        if(cur->right) searchBST(cur->right);

        return ;
    }

    vector<int> findMode(TreeNode* root) {
        result.clear();
        searchBST(root);

        return result;
    }
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.

注意:

二叉搜索树要用中序遍历才能得到一个有序的遍历数组。


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

思路:

用 后序遍历 自底向上 查找。

1.自底向上,若左右子树都不为空,该子树即为要找的最近公共祖先;

2.若右子树为空,则返回左子树;

3.若左子树为空,则返回右子树;

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == NULL || root == p || root == q) return root;

        // 后序遍历
        // 左
        TreeNode* left = lowestCommonAncestor(root->left, p, q);

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

        // 中
        if(left != NULL && right != NULL){
            return root;
        }
        if(left == NULL) return right;
        return left;
    }
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.