【leetcode】No. 235 LCABST

原创 2015年07月11日 10:20:55

Problem

这里写图片描述

Code

/**
 * 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==NULL || root==p || root==q)
        return root;
    struct TreeNode *left = lowestCommonAncestor(root->left,p,q);
    struct TreeNode *right = lowestCommonAncestor(root->right,p,q);
    if(left==NULL)  return right;
    if(right==NULL) return left;
    return root;
}

Comment

This is not hard,but interesting.

版权声明:Pain is just in your mind.

相关文章推荐

[LeetCode]235. Lowest Common Ancestor of a Binary Search Tree

Problem DescriptionGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two gi...

[leetcode] 235.Lowest Common Ancestor of a Binary Search Tree

题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in t...

leetcode 235&236

235Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the...

LeetCode 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树

235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the low...

leetcode235. Lowest Common Ancestor of a Binary Search Tree

235. Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest...

leetcode235

Lowest Common Ancestor of a Binary Search Tree
  • Ekinnn
  • Ekinnn
  • 2016年12月19日 19:13
  • 99

leetcode235题 题解 翻译 C语言版 Python版

235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the ...

LeetCode笔记:235. Lowest Common Ancestor of a Binary Search Tree

找出一个二叉查找树中两个节点的最低共同祖先

【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目...

[LeetCode]235.Lowest Common Ancestor of a Binary Search Tree

题目Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【leetcode】No. 235 LCABST
举报原因:
原因补充:

(最多只允许输入30个字)