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.

题目大意:求二叉树的两个节点的最近祖先。

解题思路:递归的来写,

设两个变量left和right,如果在root节点的左右孩子节点分别发现节点p和q,

那么假设left=q,right=p

那么root就是最近祖先;

如果left=null,right=q或p,那么right就是最近祖先;

如果right=null,left=q或p,那么left就是最近祖先。

  public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null||root==q||root==p){
return root;
}
TreeNode left = lowestCommonAncestor(root.left,p,q);
TreeNode right = lowestCommonAncestor(root.right,p,q);
if(left!=null&&right!=null){
return root;
}
return left==null?right:left;
}

也看到有人用map记录父节点信息,然后利用map找出最近公共祖先,也不错。

代码如下:

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
HashMap<TreeNode,TreeNode> m = new HashMap<TreeNode,TreeNode>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(queue.peek()!=null){
TreeNode t = queue.poll();
if(t.left!=null){
m.put(t.left,t);
queue.offer(t.left);
}
if(t.right!=null){
m.put(t.right,t);
queue.offer(t.right);
}
}
Set<TreeNode> l = new HashSet<TreeNode>();
TreeNode pp = p;
while(pp!=root){
l.add(pp);
pp = m.get(pp);
}
l.add(root);
TreeNode qq = q;
while(!l.contains(qq)){
qq = m.get(qq);
}
return qq;
}

Lowest Common Ancestor of a Binary Tree——Leetcode的更多相关文章

  1. Lowest Common Ancestor of a Binary Tree leetcode

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

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

  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

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

  6. 88 Lowest Common Ancestor of a Binary Tree

    原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...

  7. [LeetCode] 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 Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  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. WCF学习笔记一(概述)

    WCF  Windows Communication Foundation 分布式通信框架.WCF是对现有分布式通信技术的整合.是各种分布式计算的集大成者.主要整合技术如下图: WCF的服务不能孤立的 ...

  2. 训练趣题:黑与白 有A、B、C、D、E五人,每人额头上都帖了一张黑或白的纸。(此处用javascript实现)

    今天的题目原题是这样的: “ 黑与白:有A.B.C.D.E五人,每人额头上都帖了一张黑或白的纸.五人对坐,每人都可以看到其它人额头上的纸的颜色.五人相互观察后,A说:“我看见有三人额头上帖的是白纸,一 ...

  3. latch: cache buffers chains故障处理总结

    一大早就接到开发商的电话,说数据库的CPU使用率为100%,应用相应迟缓.急匆匆的赶到现场发现进行了基本的检查后发现是latch: cache buffers chains 作祟,处理过程还算顺利,当 ...

  4. ORACLE如何停止一个JOB

    ORACLE如何停止一个JOB1 相关表.视图2 问题描述为同事解决一个因为网络连接情况不佳时,执行一个超长时间的SQL插入操作.既然网络状况不好,就选择了使用一次性使用JOB来完成该插入操作.在JO ...

  5. OC调用Swift 整理步骤!总结别人的!方便自己查找!

    1. 2. 上面的修改了一个配置项,有一个Product Module Name在后面会使用. 在工程里面点击File/New/File…,选择iOS/Source/Cocoa Touch Class ...

  6. 类库探源——System.Type

    一.MSDN 描述 Type 类:表示类型声明:类类型.接口类型.数组类型.值类型.枚举类型.类型参数.泛型类型定义.以及开放或封闭构造的泛型类型. 命名空间: System 程序集:mscorlib ...

  7. ZOJ 1057 Undercut(简单模拟)

    Undercut 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=57 题目大意:a card game,two pla ...

  8. ASP.Net定时任务执行

    原料: System.Timers.Timer():通过.NET  Thread  Pool实现的,轻量,计时精确,对应用程序.消息没有特别的要求:缺点是不支持直接的拖放,需要手工编码. Timer的 ...

  9. 用frame实现最基本的上中下三层布局,中间又分左右两部分.

    用frame实现最基本的上中下三层布局,中间又分左右两部分. 用frame的好处在于不用象DIV一样要对浮动和大小进行精确控制,以及要考虑宽屏的时候怎么办.而且在导航的时候非常简单.比如说,左边是导航 ...

  10. MySql模糊查询like通配符使用详细介绍

    MySQL提供标准的SQL模式匹配,以及一种基于象Unix实用程序如vi.grep和sed的扩展正则表达式模式匹配的格式. 一.SQL模式 SQL的模式匹配允许你使用“_”匹配任何单个字符,而“%”匹 ...