首页 > 其他 > 详细

LCA

时间:2020-09-22 09:12:47      阅读:45      评论:0      收藏:0      [点我收藏+]

LeetCode 235

题目链接:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/

题目代码:

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        TreeNode *cur = root;
        while (1) {
            if (p -> val < cur -> val && q -> val < cur -> val)
                cur = cur -> left;
            else if (p -> val > cur -> val && q -> val > cur -> val)
                cur = cur -> right;
            else
                break;
        }
        return cur;
    }
};

 

LCA

原文:https://www.cnblogs.com/realxjr/p/13710013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!