题目:

Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes.

The lowest common ancestor is the node with largest depth which is the ancestor of both nodes.

Example

For the following binary tree:

  4
/ \
3 7
/ \
5 6

LCA(3, 5) = 4

LCA(5, 6) = 7

LCA(6, 7) = 7

题解:

  左右子树中找到了p和q才返回该root节点,否则返回nullptr

  因为题目中设定两节点一定存在,对于6和7节点,7节点直接返回,不会继续在其左右子树中寻找6,因为此时对于4来说,左子树应该返回nullptr,那么p和q一定在右子树中,只要找到一个节点即可,另一个节点一定在第一个节点的子树中(不是很严谨,因为还有5,6情况,此情况下对于7,左右子树皆不为空,此时7返回的是自身)

Solution 1 ()

class Solution {
public:
/*bool helper(TreeNode *root,TreeNode *p){
if(root==nullptr) {
return false;
}
if(root==p) {
return true;
} return helper(root->left,p) || helper(root->right,p);
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(helper(root->left,p) && helper(root->left,q)) {
return lowestCommonAncestor(root->left,p,q);
}
if(helper(root->right,p) && helper(root->right,q)) {
return lowestCommonAncestor(root->right,p,q);
} return root;
}*/
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root==NULL || root==p || root==q) {
return root;
} TreeNode *left = lowestCommonAncestor(root->left, p, q);
TreeNode *right = lowestCommonAncestor(root->right, p, q);
if (left != NULL && right != NULL) {
return root;
}
if (left != NULL) {
return left;
}
if (right != NULL) {
return right;
}
return NULL;
}
};

【Lintcode】088.Lowest Common Ancestor的更多相关文章

  1. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【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 th ...

  3. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  4. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)

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

  5. 【leetcode】1123. Lowest Common Ancestor of Deepest Leaves

    题目如下: Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall th ...

  6. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  7. 【easy】235. Lowest Common Ancestor of a Binary Search Tree

    题意大概是,找两个节点的最低公共祖先 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod ...

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

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

  9. 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

随机推荐

  1. java nio读取和写入文件

    读取 package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS ...

  2. 卸载gnu gcj

    麻辣个鸡的,我在Linux上安装的jkd版本是1.8,然后可能是之后安装了GCC吧,他大爷的,java版本变成了1.5.这个残酷的事实是在我写练习Package的测试文件的时候搞得. 机智的看了一下j ...

  3. php正则表达式匹配img中任意属性的方法

    经常和图片打交道,不得不用到一些提取图片中scr.alt.title.等的属性,这里总结给大家一些常用的,感觉还不错,比较通用! PHP正则表达式匹配img中任意属性PHP 复制代码代码如下: < ...

  4. HDU 5338(ZZX and Permutations-用线段树贪心)

    ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  5. python之Matplotlib 和Numpy

    1.matplotlib http://www.cnblogs.com/TensorSense/p/6802280.html https://wenku.baidu.com/view/e1c15c9d ...

  6. 【百度之星复赛】T5 Valley Numer

    Valley Numer Problem Description 众所周知,度度熊非常喜欢数字. 它最近发明了一种新的数字:Valley Number,像山谷一样的数字. 当一个数字,从左到右依次看过 ...

  7. 双缓冲类里的OnPaint与OnSize,以及构造函数的关系

    代码摘自wx\lib\agw\knobctrl.py一点体会是,OnSize作为class的函数,被放在构造函数里执行,会先于OnPaint执行.测试结果是,初始启动后,会执行8次OnSize(为什么 ...

  8. 关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法

    文章转载自:关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法 | TeachCourse

  9. ERR:/usr/local/lib/libcrypto.so.1.0.0: no version information available

    解决方法: locate libssl.so.1.0.0   sudo rm /usr/local/lib/libssl.so.1.0.0   sudo ln -s /lib/x86_64-linux ...

  10. <raspberry pi > 用树莓派来听落网电台

    树莓派放在抽屉里吃灰有半年多了,去年玩了1个月后就没怎么开整了,上个月没工作,刚好有点闲暇,就把树莓派翻出来折腾,刚好碰到落网改版了,想起以前在树莓派论坛看到有网友拿树莓派来听豆瓣电台,代码那时我都下 ...