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. Python3.5-20190526-廖老师-自我笔记-单元测试-参数换-paramunittest

    参数化: import timeimport list1 #想测试list1中的求和函数是否正确fun1import paramunittestimport unittest #先设置参数组@para ...

  2. python+django+pycharm 环境配置 (window7)

    一.python环境配置 登录python官网,下载windows版的python,本项目使用32位的python2.7.6,下载地址: http://www.python.org/ftp/pytho ...

  3. 使用字节流(InputStream、OutputStream)简单完成对文件的复制

    文件的复制 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; imp ...

  4. [原创] Delphi InputBox、InputQuery函数

    Delphi InputBox.InputQuery函数 两个函数都是弹框提示输入信息 function InputQuery(const ACaption, APrompt: string; var ...

  5. 第一周作业—N42-虚怀若谷

    一.Linux发行版描述. Linux发行版主要有三个分支:Slackware.Debian.Redhat: (1) Slackware: SUSE:基于Slackware二次开发的一款Linux,主 ...

  6. 如何在MaxCompute上处理存储在OSS上的开源格式数据

    0. 前言 MaxCompute作为使用最广泛的大数据平台,内部存储的数据以EB量级计算.巨大的数据存储量以及大规模计算下高性能数据读写的需求,对于MaxCompute提出了各种高要求及挑战.处在大数 ...

  7. UIView的 形变属性transform

    // ViewController.m // 形变属性transform // // Created by LiuWei on 2018/4/23. // Copyright © 2018年 xxx. ...

  8. shell脚本学习 (10) 从结构化文本提取数据

    1提取/ 后的数据 sed -e 's=/.*==' do.txt 2 sed -e 's=/.*=='\ -e 's=^\([^:]*\):\(.*\) \([^ ]*\)=\1:\3, \2=' ...

  9. 【CF1210B】Marcin and Training Camp(贪心)

    题意:有n个人,60种技能点,如果第i个人会第j种技能a[i]的二进制表示的第j位就是1,第i个人的价值是b[i] 如果有若干种技能i会j不会,i就会鄙视j 求一种至少两个人的选人方案使得价值和最大, ...

  10. C# 调用windows时间同步服务获取准确时间

    //创建一个Daytime类代码如下:using System; using System.Collections; using System.Collections.Generic; using S ...