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

如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能是一个节点的祖先,也可能是两个节点的祖先,如果是一个节点的祖先,那么公共祖先还在上边,还需要返回结果进行判断,如果是两个节点的祖先,那么最小公共祖先就是这个或者在下边,总之,返回有结果的那个子树就是了。

所以思路就是,往下遍历,直到找到节点然后返回,返回以后判断此时左右子树的情况,根据情况返回根节点的递归结果或者子树的递归结果

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
/*
从底下开始判断,两个节点是不是在左右子树上
*/
if(root==null) return null;
//如果遍历到了某个节点,说明这个节点在这个子树上,返回这个节点
if (root==p||root==q) return root;
//如果还没遍历到,那么公共节点肯定在下边,继续往下寻找左子树和右子树
//返回的两个分别是左、右子树上遍历到的某个节点的祖先
TreeNode lt = lowestCommonAncestor(root.left,p,q);
TreeNode rt = lowestCommonAncestor(root.right,p,q);
//如果左右子树都找到了,那么这个节点就是公共祖先
if (lt!=null&&rt!=null) return root;
//如果有空缺,那么返回那个找到的,这种情况是到现在为止,两个节点已经在一棵子树上了,返回以当前子树为起点的运算结果就行
return (lt==null)?rt:lt;
}

[leetcode]236. 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. [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 ...

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

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

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

  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. (medium)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二叉树最近公共祖先

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

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

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

随机推荐

  1. python删除list中的空list

    list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText'] 如何删除空列表,以便我得到: list2 = ['text', 'text2 ...

  2. C语言const和define的区别

    const 定义的是变量不是常量,只是这个变量的值不允许改变是常变量!带有类型.编译运行的时候起作用存在类型检查. define 定义的是不带类型的常数,只进行简单的字符替换.在预编译的时候起作用,不 ...

  3. PyQt(Python+Qt)学习随笔:QScrollArea的alignment属性不起作用的原因

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 Scroll Area滚动区域提供了一个呈现在其他部件上的可滚动区域视图,对应类为QScrollAr ...

  4. PyQt(Python+Qt)学习随笔:QTableView的cornerButtonEnabled属性

    老猿Python博文目录 老猿Python博客地址 cornerButtonEnabled属性用于控制是否启用左上角的按钮.如果此属性为True,则启用视图左上角的按钮,单击此按钮将选择表视图中的所有 ...

  5. SELECT 1,2,3...的含义及其在SQL注入中的用法

    首先,select 之后可以接一串数字:1,2,3-只是一个例子,这串数字并不一定要按从小到大排列,也不一定从1开始,这串数字的值和顺序是任意的,甚至可以是重复的,如:11,465,7461,35 或 ...

  6. python——pandas技巧(处理dataframe每个元素,不用for,而用apply)

    用apply处理pandas比用for循环,快了无数倍,测试如下: 我们有一个pandas加载的dataframe如下,features是0和1特征的组合,可惜都是str形式(字符串形式),我们要将其 ...

  7. Java基础学习之面向对象(4)

    目录 1.面向对象概述 1.1.类与对象的关系 1.2.类的具体描述 2.面向对象的三大特性 2.1.继承 2.2.多态 2.3.封装 1.面向对象概述 1.1.类与对象的关系 有对象吗,没有的话我给 ...

  8. 题解-CF348E Pilgrims

    题面 CF348E Pilgrims 有一棵 \(n\) 个点的 带权 树和 \(m\) 个关键点,要求杀了一个不关键的点,满足最多的关键点到离它最远的所有关键点的路径都被打断.求可以满足的最多关键点 ...

  9. rhel6.4搭建rac前共享存储配置(iscsi+multipath+udev)

    rhel6.4搭建rac前共享存储配置(iscsi+multipath+udev) server: IP配置: 192.168.12.30 192.168.12.40   添加一个100G磁盘/dev ...

  10. JavaSE01-概述

    1.1 Java语言发展史 语言:人与人交流沟通的表达方式 计算机语言:人与计算机之间进行信息交流沟通的一种特殊语言 Java语言是美国Sun公司(Stanford University Networ ...