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

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

===============

CLA问题,

利用递归方式:采用的后序遍历的思想,LRN

左子树查找,右子树查找,这个思路进行.

TreeNode *seek(TreeNode *root,TreeNode *p,TreeNode *q){
help_seek();
}
void help_seek(TreeNode *root, TreeNode *p,TreeNode *q,bool l,bool r){
if(r && l) cout<<"find the value""<<root->val<<endl;
if(root){
help_seek(root->left);
help_seek(root->right);
}
}

???????

====

代码:

/**
* 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==nullptr) return NULL;
if(root==p || root==q) return root;
TreeNode *left_lca = lowestCommonAncestor(root->left,p,q);
TreeNode *right_lca = lowestCommonAncestor(root->right,p,q);
if(left_lca&&right_lca) return root; return left_lca==nullptr? right_lca:left_lca;
}
};

236. Lowest Common Ancestor of a Binary Tree的更多相关文章

  1. leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  2. Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium

    236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...

  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】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 ...

  5. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

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

  6. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先

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

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

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

  8. leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)

    https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...

  9. leetcode 236. Lowest Common Ancestor of a Binary Tree

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

随机推荐

  1. Pickpic和FarStone走好..GreenShot上岗

    很早前就看過這丫的,以前就是拒絕改變,換過n多切圖工具了,除了題目中倆 還自己用AHK過一款,但最後還是停在Pickpic因為要上FTP比較快 今天在SourceForge亂逛邂逅了這貨,才知道原來” ...

  2. 我对CSS vertical-align的一些理解与认识(一)

    一.关于今天,本文,及其他 今天是个特殊的日子,因为今天是汶川地震两周年的日子,我很悲鸣:今天又是国际护士节,看到微博上护士照横流,我很欣慰. 一段放松的YY后,进入正题.上个月21号,有位同行留言想 ...

  3. Crawler4j学习笔记

    Crawler4j概述 crawler4j是一款基于Java的轻量级单机开源爬虫框架,最大的一个特点就是简单.另外也支持多线程.支持代理.可以过滤重复URL 基本上从加载jar到工程里面 通过修改示例 ...

  4. 水灾(sliker.cpp/c/pas) 1000MS 64MB

    大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY所在的城市可以用一个N*M(N,M<=50)的地图表示,地图上 ...

  5. 创建kafkatopic和productor

    cd 到kafka 目录下 创建topic create topicbin/kafka-topics.sh --zookeeper spark1:2181,spark2:2181,spark3:218 ...

  6. 在线音乐API的研究 (Part 2.1)

    本文转载于:http://www.cnblogs.com/osmondy/p/LyricApi.html 最近,在优化一个自己写的音乐播放器.主要目的是回顾.归纳,并希望能够写出一个属于自己的comm ...

  7. 安装postgreSQL出现configure:error:readline library not found解决方法

    要安装 readline , readline-dev 开发包,要么使用 --without-readline 选项关闭 readline 功能. #yum install readline; #yu ...

  8. shell之脚本练习

    脚本需求集合贴-自主开发的 对频繁执行的任务有编写脚本的价值 对单次执行的任务就用笨的,简单的办法 1.对asterisk写一个脚本 查日志 输入日期--能够输出对应日期的日志 输入多个条件--能够输 ...

  9. C编码-1

    两个关键点,一个是要懂C语法,另一个要懂业务知识,即能够分解问题 字节序测试程序 不同cpu平台上字节序通常也不一样,下面写个简单的C程序,它可以测试不同平台上的字节序. 网络字节序说是大端字节序. ...

  10. Android Afinal框架

    项目如图: 本文参考网络! Afinal是一个开源的android的orm和ioc应用开发框架,其特点是小巧灵活,代码入侵量少.在android应用开发中,通过 Afinal的ioc框架,诸如ui绑定 ...