码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 235 Lowest Common Ancestor of a Binary Search Tree

时间:2015-11-16 09:28:43      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

LeetCode 235 Lowest Common Ancestor of a Binary Search Tree

 

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    if(!root||!p||!q)
        return NULL;
    if(root->val < p->val && root->val < q->val)
        return lowestCommonAncestor(root->right,p,q);
    else if(root->val > p->val && root->val > q->val)
        return lowestCommonAncestor(root->left,p,q);
    else return root;
}

 

LeetCode 235 Lowest Common Ancestor of a Binary Search Tree

标签:

原文地址:http://www.cnblogs.com/walker-lee/p/4968030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!