• LeetCode-剑指68-I.二叉搜索树的最近公共祖先


    在这里插入图片描述

    1、深度遍历

    我们可以利用一个函数确定待搜索的节点是否在当前节点构成的子树中。而后我们可以从根节点开始遍历:1、若两个节点同属于其左子树或右子树构成的节点,说明当前节点不是公共节点,我们移动至当前节点的左子节点或右子节点;2、若此时两个节点分属于不同的节点,说明当前的节点即为其最近公共祖先

    class Solution {
    public:
        bool in(TreeNode *root, TreeNode *target) {
            if (!root) return false;
            if (root->val == target->val) return true;
            else if (root->val > target->val) return in(root->left, target);
            else return in(root->right, target);
        }
    
        TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {
            TreeNode *res = root;
            while (res) {
                if (in(res->left, p) && in(res->left, q)) res = res->left;
                else if (in(res->right, p) && in(res->right, q)) res = res->right;
                else break;
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2、深度遍历优化

    我们可以对方法一进行优化,在一次遍历中查询当前节点是否为p和q的公共祖先:1、若当前节点的值等于p或q的值说明当前节点就是公共祖先;2、若当前节点的值大于或小于p和q的值则说明当前节点的左子节点或右子节点为其公共祖先;3、若不满足上述情况说明当前节点即为公共祖先。

    class Solution {
    public:
        TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {
            if (root->val == p->val || root->val == q->val) return root;
            if (root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left, p, q);
            if (root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right, p, q);
            return root;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    C# EPPlus导出dataset----Excel4其他设置
    大模型为使用Prompt提供的指导和建议
    两行CSS让页面提升了近7倍渲染性能!
    学完性能测试理论和数据模拟就能拿下15k?你敢信?
    【Ansible】playbook剧本
    LeetCode讲解篇之面试题 01.08. 零矩阵
    美国网站服务器解决方案
    教师资格证报名浏览器不兼容 - 解决方案
    Docker安装RabbitMQ
    HBuilderX vue项目打包上传到服务器
  • 原文地址:https://blog.csdn.net/weixin_43825194/article/details/128017325