【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree
- 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 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.
解 对于root节点,如果左右子树分别有一个节点,则root是一个公共祖先
解1 递归
class Solution {
public:
TreeNode *ans;
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
LCA(root, p, q);
return ans;
}
bool LCA(TreeNode *root, TreeNode *p, TreeNode *q){
if(root == NULL)return false;
int l = LCA(root->left, p, q) ? 1 : 0; // p或者q在左子树
int r = LCA(root->right, p, q) ? 1 : 0; // p或者q在右子树
int mid = (root == p || root == q) ? 1 : 0; //找到了p或者q
if(mid + l + r >= 2)ans = root; // =2时,左右各一个;>2时,一个是另一个的先驱节点
return mid + l + r > 0; // >0表明找到了p或者q
}
};
【刷题-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 ...
- 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 ...
- Java for LeetCode 236 Lowest Common Ancestor of a Binary Tree
解题思路一: DFS到每个节点的路径,根据路径算出LCA: public class Solution { public TreeNode lowestCommonAncestor(TreeNode ...
- LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- [leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先
如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...
随机推荐
- CF1491A K-th Largest Value 题解
Content 你有一个长度为 \(n\),并且仅包含 \(0/1\) 的数组 \(a\).现在对这个序列做以下两种操作之一共 \(q\) 次: \(1\) \(x\):将 \(a_x\) 修改为 \ ...
- Jquery监控audio单选框选中事件(实际通过click)
$('input:radio[name="pathType"]').click(function(){ var checkValue = $('input:radio[name=& ...
- vim 默认配置
vim ~/.vimrc 然后输入常用的需要用的命令,然后保存,这个每次启动VIM都会自动配置.当然你也可以在VIM里面按":"之后输入如下命令,但是下次启动之后设置就会丢失了. ...
- SpringBoot打包实现静态文件、配置文件、jar包分离
在pom文件里面添加 <plugins> <!--定义项目的编译环境--> <plugin> <groupId>org.apache.maven.plu ...
- HttpServletResponse工具类和HttpServletRequest工具类,前台参数接收方式和后台返回(JSON)数据格式
RequestUtils.java 操作类 package cn.utils; import org.apache.commons.lang3.StringUtils; import org.slf4 ...
- 【LeetCode】1151. Minimum Swaps to Group All 1's Together 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...
- 【LeetCode】225. Implement Stack using Queues 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- anaconda 如何更换镜像源
今天需要对anaconda更换其镜像源. 故而做一个小记: 一 查看anaconda的本源方法 电脑路径:C:\Users\14269,找到 .condarc 文件. 打开.condarc文件,可看 ...
- Java线程安全MAP ,LIST ,SET
ConcurrentHashMap是线程安全的HashMap, CopyOnWriteArrayList是线程安全的ArrayList CopyOnWriteArraySet是线程安全的Set.
- JUC之线程间的通信
线程通信 视频1: 2021.12.18 JUC视频学习片段 对上次多线程编程步骤补充(中部): 创建资源类,在资源类中创建属性和操作方法 在资源类里面操作 判断 干活 通知 创建多个线程,调用资源类 ...