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.

这个题目一开始很是困惑~不知道该如何下手。发现别人的解答很是简单,发现递归真是一门学问啊!

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null) return null;
if(root == p || root == q) return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
return left != null && right != null ? root : left == null ? right : left;
} }

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

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

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

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

  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 OJ: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 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 利用二 ...

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

  8. [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 ...

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

随机推荐

  1. vue 相对其他热门 框架 优点 --- 待续

    react vs  vue 1. 处理动画 vue 更有优势 , 这是由于 React 有大量的检查机制 2.性能更高, 在React中,你需要在每个地方去实现 shouldComponentUpda ...

  2. VS编译wxWidgets

    准备工作 下载wxWidgets源码包(官网),我用的是3.02版: 安装Visual Studio.我用的是VS 2015 RC: 编译源码 解压wxWidgets的源码包,会得到一大堆文件.进入b ...

  3. Java内存一致性

    问题 前段时间在做服务注册发现的时候,有一处这样的逻辑,先发现下游服务,然后再是服务启动,服务启动完毕才能注册服务,因为注册一个在启动过程中的服务显然是不正确的设计. 然而很不巧,我们目前使用的TTh ...

  4. IIS的php环境配置

    session保存 再找到 session.save_path = /tmp ,将其改为 session.save_path = C:\php-4.3.2-Win32\sessions .(注:此时你 ...

  5. Spring 上传文件

    最近碰到一个上传文件的需求,其实之前也做过但是都是search->copy 没有细究过,这次纯手工. 先看一下需要依赖的包: <dependency> <groupId> ...

  6. pull类型消息中间件-消息发布者(一)

    消息集群架构 对于发送方来说的关键几要素 topic 消息的主题,由用户定义.类似于知乎的话题,Producer发送消息的时候需要指定发送到某一个topic下面,Consumer从某一个topic下面 ...

  7. memcached and redis

    http://hzp.iteye.com/blog/1872664 http://www.diggerplus.org/archives/190 Redis

  8. html5权威指南:表格元素

    第十一章:表格元素                                                                                           ...

  9. mobiscroll 插件札记(一)

    mobiscroll 插件笔记(一) 文章参照  http://www.cnblogs.com/headwolf/archive/2013/12/23/3487207.html 最近切一个移动页面,需 ...

  10. MVC5 + EF6酒店项目笔记

    最近项目组准备用MVC5开发酒店模板包括后台.在此第一次学MVC5一个礼拜,看着组长给我的MVC5模板从一脸懵逼到懵懂. 慢慢学习,成长. 未完待续........