51.Lowest Common Ancestor of a Binary Tree(二叉树的最小公共祖先)
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(二叉树的最小公共祖先)的更多相关文章
- [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树的最小公共祖先
如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...
- [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 ...
- [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 ...
- 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 利用二 ...
随机推荐
- htmlunit填坑
htmlunit 无头浏览器 爬虫使用填坑: <!-- htmlunit start --> <dependency> <groupId>org.jsoup< ...
- pugixml的使用
VS项目,头文件处鼠标右键,添加“新建筛选器”,重命名为pugixml,把3个文件添加进来.在用到框架的文件中只需#include"pugixml\pugixml.hpp"即可. ...
- Linux系统下安装JDK及环境配置
第一种属于傻瓜式安装,一键安装即可(yum安装): 第二种手动安装,需要自己去Oracle官网下载需要的jdk版本(需官网注册登录才可以下载),然后解压并配置环境. 一.yum一键安装1.首先执行以下 ...
- 51nod 1040:最大公约数之和(数论)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1040 给出一个n,求1-n这n个数,同n的最大公约数的和. ...
- 【RabbitMQ】Concurrency、Prefetch、exclusive
分布式消息中间件 RabbitMQ是用Erlang语言编写的分布式消息中间件,常常用在大型网站中作为消息队列来使用,主要目的是各个子系统之间的解耦和异步处理.消息中间件的基本模型是典型的生产者-消费者 ...
- XP定位(APP元素定位)
Appium app自动化测试经验分享-Xpath定位总结 在我看来,自动化测试中元素定位的倚天剑和屠龙刀莫过于 Xpath和CSS,但CSS只用于Web(之前已经分享过),这次就分享下Xpath的定 ...
- dubbo-go 的开发、设计与功能介绍
dubbo-go 的前世今生 dubbo-go 是目前 Dubbo 多语言生态最火热的项目.dubbo-go 最早的版本应该要追溯到 2016 年,由社区于雨同学编写 dubbo-go 的初版.当时很 ...
- SpringMVC的 ModelAndView
使用ModelAndView类用来存储处理完后的结果数据,以及显示该数据的视图. public class ModelAndView { /** View instance or view name ...
- STM32输入捕获TIM2四通道
相比于一通道,原子的例程里因为清了计数时间,所以要对程序进行修改. 记录上升沿后的计数,然后记录下降沿的计数.相减后计算高电平时间,对于定时器中断间隔的边界要分开处理. 这里因为我的接收机时间是1ms ...
- python数据储存
python数据储存 csv文件的操作 安装csv包打开cmd 执行 pip install csv引入的模块名为csv 读取文件 with open("xx.csv"," ...