[leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先
如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的。
如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能是一个节点的祖先,也可能是两个节点的祖先,如果是一个节点的祖先,那么公共祖先还在上边,还需要返回结果进行判断,如果是两个节点的祖先,那么最小公共祖先就是这个或者在下边,总之,返回有结果的那个子树就是了。
所以思路就是,往下遍历,直到找到节点然后返回,返回以后判断此时左右子树的情况,根据情况返回根节点的递归结果或者子树的递归结果
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树的最小公共祖先的更多相关文章
- [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 ...
- [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 ...
- 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...
- 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 ...
- 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 ...
- (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 ...
- [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 ...
- [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 ...
- LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
随机推荐
- Intel s2600系列做虚拟化需要注意的item
Intel s2600cw\cp主板做虚拟化BIOS需要设置以下选项,附件为截图供参考. -->vt,cpu虚拟化,默认关闭,需要确保开启 -->ht,cpu超线程,默认开启,需要确保开启 ...
- 16.java设计模式之迭代器模式
基本需求: 展示一个学校的结构,比如一个学校下面有多个学院,学院下面有多个系,对其节点主要是遍历,与组合模式略有不同 传统方案: 学校<-学院<-系 依次继承 这种方式,在一个页面中展示出 ...
- LeetCode 040 Combination Sum II
题目要求:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find al ...
- oracle set oracle_sid=xxxxxx
本地有多个实例,在cmd 输入 set oracle_sid=xxxxx 来指定要连接的实例 sqlplus xxxx/xxxx@1.1.1.1.1/sid 连接数据库
- SpringBoot集成FastDFS依赖实现文件上传
前言 对FastDFS文件系统安装后的使用. FastDFS的安装请参考这篇:Docker中搭建FastDFS文件系统(多图) 本文环境:IDEA + JDK1.8 + Maven 本文项目代码:ht ...
- Kotlin for Java Developers 学习笔记
Kotlin for Java Developers 学习笔记 ★ Coursera 课程 Kotlin for Java Developers(由 JetBrains 提供)的学习笔记 " ...
- 排序--ShellSort 希尔排序
希尔排序 no 实现 希尔排序其实就是插入排序.只不过希尔排序在比较的元素的间隔不是1. 我们知道插入排序 都是 一个一个和之前的元素比较.发现比之前元素小就交换位置.但是希尔排序可能是和前第n个元素 ...
- 使用Fiddle修改请求数据
修改请求数据 以淘宝网为例 命令行中输入bpu 及要拦截的请求地址,如bpu https://s.taobao.com/search 在搜索栏中输入"面包机" 可以看到拦截到的请求 ...
- 团队作业part2--需求规格说明书
需求规格说明书 一.目的 本说明书为了让用户尽快了解产品所含功能,描述用户对产品的期望与需求.明确软件开发的最终目的,使开发出来的软件能够更好的达到用户的需求.其作为用户和软件开发人员达成的技术协议书 ...
- 【学习笔记】动态 dp 入门简易教程
序列 dp 引入:最大子段和 给定一个数列 \(a_1, a_2, \cdots, a_n\)(可能为负),求 \(\max\limits_{1\le l\le r\le n}\left\{\sum_ ...