Level:

  Medium

题目描述:

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 p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

Note:

  • All of the nodes' values will be unique.
  • p and q are different and both values will exist in the binary tree.

思路分析:

  在一个二叉树中寻找两个节点的最小公共祖先,如果根不为空时,不断从其左右子树搜索,如果两个节点都在左子树,就在左子树递归查找,如果两个节点都在右子树,就在右子树递归查找,如果一个节点在左子树,一个节点在右字数,那么当前节点就是最小公共祖先。

代码:

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||root==p||root==q)
return root;
//查看左子树有没有目标节点,没有就为null
TreeNode left=lowestCommonAncestor( root.left, p,q);
//查看右子树有没有目标节点,没有就为null
TreeNode right=lowestCommonAncestor( root.right, p,q);
//都不为空说明左右子树都有目标节点,那么当前节点就是最小祖先
if(left!=null&&right!=null)
return root;
//左子树为空就去右子树找,否则去左子树找
return left!=null?left:right;
}
}

51.Lowest Common Ancestor of a Binary Tree(二叉树的最小公共祖先)的更多相关文章

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

  2. 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先

    给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...

  3. [leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先

    如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...

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

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

  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 二叉树两个子节点的最低公共父节点

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

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

随机推荐

  1. A Tutorial on Using the ALSA Audio API

    A Tutorial on Using the ALSA Audio API This document attempts to provide an introduction to the ALSA ...

  2. Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)

    怎么老是垫底啊. 不高兴. 似乎 A 掉一道题总比别人慢一些. A. Paint the Numbers 贪心,从小到大枚举,如果没有被涂色,就新增一个颜色把自己和倍数都涂上. #include< ...

  3. golang-练习2

    反转字符串 package main import "fmt" func FirstReverse(str string) string { var str1 []rune run ...

  4. Java web入门之Http请求和响应

    三层架构 web层:JSP + Servlet.Struts 2.SpringMVC service层:Spring dao层:JDBC.DBUtils.Hibernate.MyBatis form表 ...

  5. springboot支持webSocket和stomp实现消息订阅通知示例

    先导入支持websocket的jar包,这里用Gradle构建的项目: dependencies { compile('org.springframework.boot:spring-boot-sta ...

  6. Jenkins服务配置容易忽略的事项

    git客户端必须安装(可直接yum安装) maven安装的版本(Jenkins上用其插件较稳健,亲测maven3.5是坑) settings.xml文件必要时,指定对应路径(一般选用Jenkins默认 ...

  7. vue 全局 js 方法

    1.新增 getCurrentDataType.js 文件 import cookieUtils from '@/config/cookieUtils' function getCurrentData ...

  8. 改计算机名导致 Oracle因目标主机或对象不存在

    手贱修改了计算机名, 结果导致登陆oracle数据库报如下错误,一查资料,说是修改了计算机名导致的,需要进到oracle安装目录: \oracle\product\10.2.0\db_1\NETWOR ...

  9. 从React Native到微服务,落地一个全栈解决方案

    Poplar是一个社交主题的内容社区,但自身并不做社区,旨在提供可快速二次开发的开源基础套件.前端基于React Native与Redux构建,后端由Spring Boot.Dubbo.Zookeep ...

  10. Codeforces 803F - Coprime Subsequences(数论)

    原题链接:http://codeforces.com/contest/803/problem/F 题意:若gcd(a1, a2, a3,...,an)=1则认为这n个数是互质的.求集合a中,元素互质的 ...