Day21 二叉搜索树的最小绝对差 + 二叉搜索树中的众数 + 二叉树的最近公共祖先

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

题目链接:530. 二叉搜索树的最小绝对差 - 力扣(LeetCode)

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

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

思路:
由于是二叉搜索树,所以中序遍历是有序的,只需要比较相邻两个元素的差值是否最小即可,因此需要保存一个pre节点。

class Solution {
public:
    void inorder(TreeNode* cur, TreeNode*& pre, int& ans)
    {
        if(cur == nullptr) return;
        inorder(cur->left, pre, ans);
        if(pre != nullptr)
        {
            ans = min(ans, abs(pre->val - cur->val));
        }
        pre = cur;
        inorder(cur->right, pre, ans);
        return;
    }

    int getMinimumDifference(TreeNode* root) {
        int ans = INT_MAX;
        TreeNode* pre = nullptr;
        inorder(root, pre, ans);
        return ans;
    }
};

501 二叉搜索树中的众数

题目链接:501. 二叉搜索树中的众数 - 力扣(LeetCode)

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

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

思路:
使用map存储元素个数,遍历map,更新当前最大的count,获取到最终ans。

class Solution {
public:
    unordered_map<int, int> H; //num-count
    void preOrder(TreeNode* cur)
    {
        if(cur == nullptr) return;
        H[cur->val]++;
        preOrder(cur->left);
        preOrder(cur->right);
    }
    vector<int> findMode(TreeNode* root) {
        preOrder(root);
        int count = 0;
        vector<int> ans;
        for(auto it = H.begin(); it != H.end(); it++)
        {
            if(it->second > count)
            {
                count = it->second;
                ans.clear();
                ans.push_back(it->first);
            }
            else if(it->second == count)
            {
                ans.push_back(it->first);
            }
        }
        return ans;
    }
};

236 二叉树的最近公共祖先

题目链接:
236. 二叉树的最近公共祖先 - 力扣(LeetCode)

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

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

方法1 回溯

我们期待从下往上遍历,但二叉树只能从上往下遍历,使用后序遍历即可实现了从下往上。

建议观看视频:
【自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先】 https://www.bilibili.com/video/BV1jd4y1B7E2/?share_source=copy_web&vd_source=67f8ac2fd76a639f9a7ead01858bdb2d

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

方法2 记录父亲

遍历1次,记录每个节点的父节点。记录p节点的路径,再遍历q节点父亲,直到找到p路径上的节点。

class Solution {
public:
    unordered_map<int, TreeNode*> fa;
    unordered_map<int, bool> vis;
    void dfs(TreeNode* root)
    {
        if(root->left != nullptr)
        {
            fa[root->left->val] = root;
            dfs(root->left);
        }
        if(root->right != nullptr)
        {
            fa[root->right->val] = root;
            dfs(root->right);
        }
    }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        fa[root->val] = nullptr;
        dfs(root);
        while(p != nullptr)
        {
            vis[p->val] = true;
            p = fa[p->val]; 
        }
        while(q != nullptr)
        {
            if(vis[q->val]) return q;
            q = fa[q->val];
        }
        return nullptr;
    }
};
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值