Leetcode:235. 二叉搜索树的最近公共祖先

Leetcode:235. 二叉搜索树的最近公共祖先

Leetcode:235. 二叉搜索树的最近公共祖先

Talk is cheap . Show me the code .

/**
 * 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==NULL) return NULL;
        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;
    }
};
posted @ 2020-02-28 17:32  Herman·H  阅读(133)  评论(0)    收藏  举报
编辑推荐:
· 编码之道,道心破碎。
· 记一次 .NET 某发证机系统 崩溃分析
· 微服务架构学习与思考:SOA架构与微服务架构对比分析
· tomcat为什么假死了
· 聊一聊 Linux 上对函数进行 hook 的两种方式
阅读排行:
· 编码之道,道心破碎。
· 知名开源项目Alist被收购!惹程序员众怒,开团炮轰甲方
· 千万级大表,如何做性能调优?
· 记一次 .NET 某发证机系统 崩溃分析
· 如何给 GitHub Copilot "洗脑”,让 AI 精准遵循指令产出高质量代码
点击右上角即可分享
微信分享提示