LeetCode 236. 二叉树的最近公共祖先(Lowest Common Ancestor of a Binary Tree)
题目描述
给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义: “对于有根树T的两个结点u、v,最近公共祖先表示一个结点x,满足x是u、v的祖先且x的深度尽可能大。”(一个节点也可以是它自己的祖先)
_______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4
例如,节点5和节点1的最近公共祖先是节点3;节点5和节点4的最近公共祖先是节点5,因为根据定义,一个节点可以是它自己的祖先。
解题思路
考虑用前序遍历的思想,从根节点开始对左右子树递归遍历,分别记录第一个在其左右子树中找到目的节点的节点,即为最近公共祖先的节点。若该子树中不包含目的节点,则返回NULL。具体步骤如下:
- 先判断该节点是否是要寻找的两个节点,如果是则返回目的节点;
- 判断是否为空节点,若是则返回NULL;
- 递归记录左右子树找到的最近公共祖先节点;
- 若在左右子树中找到了目的节点,则说明此节点为最近的公共祖先节点,返回此结点;
- 若找到的两节点中至少一个为空,则返回不为空的节点。注意这里可分为两种情况:
- 若两节点都为空,说明左右子树均未找到目的节点,返回NULL;
- 若只有一个节点为空,说明在左右子树中只找到了一个目的节点,返回找到的那个节点。对应题目中一个节点为祖先的情况,此情况中找到的那个节点即为公共祖先
代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == p || root == q || root == NULL)
return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left && right)
return root;
return left == NULL ? right : left;
}
};
LeetCode 236. 二叉树的最近公共祖先(Lowest Common Ancestor of a Binary Tree)的更多相关文章
- [Swift]LeetCode236. 二叉树的最近公共祖先 | 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 ...
- [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium
236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...
- 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 利用二 ...
- 【刷题-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) o ...
- 【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) o ...
- 88 Lowest Common Ancestor of a Binary Tree
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
- [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实现 LeetCode 236 二叉树的最近公共祖先
236. 二叉树的最近公共祖先 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x ...
- [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 ...
随机推荐
- Kong/Konga - Docker容器化安装
1.0 安装kong + postgresDB docker network create kong-net docker pull postgres:latest docker run -d --n ...
- [转载]C++STL—vector的插入与删除
来源:https://blog.csdn.net/duan19920101/article/details/50717748 vector::erase():从指定容器删除指定位置的元素或某段范围内的 ...
- O019、通过例子学习 Keystone
参考https://www.cnblogs.com/CloudMan6/p/5373311.html 上节介绍了 Keystone 的核心概念.本节我们通过“查询可用 image”这个操作让大家对 ...
- 为什么单线程的Redis却能支撑高并发
Redis的高并发和快速原因 redis是基于内存的,内存的读写速度非常快: 核心是基于非阻塞的IO多路复用机制: redis是单线程的,反而省去了很多上下文切换线程的时间: 为什么Redis是单线程 ...
- 前端开发 | 尝试用Markdown写一下近几个月的总结
近期总结 回顾 半年前 半年前,接触了前端一年多(工作半年)的我了解的东西只有下面这些.因为在公司里的工作就是切静态页,捣鼓CMS. HTML (比较简洁的编写HTML) CSS/CSS3 (PC兼容 ...
- java对象只有值传递,为什么?
在开始深入讲解之前,有必要纠正一下大家以前的那些错误看法了.如果你有以下想法,那么你有必要好好阅读本文. 错误理解一:值传递和引用传递,区分的条件是传递的内容,如果是个值,就是值传递.如果是个引用,就 ...
- zabbix 启到不起来:active check configuration update from [127.0.0.1:10051] started to fail (cannot connect to [[127.0.0.1]:10051]: [111] Connection refused)
cat /var/log/zabbix_agent_log 查看日记出现报错:active check configuration update from [127.0.0.1:10051] star ...
- oracle 数据库启动停止小结
---登录sqlplus sqlplus /nolog conn / as sysdba shutdown immediate --启动数据库有两种方式 startup 会自动完成重启数据库的所有步 ...
- jquery前端问题随记
1.图片名称不变,但要求刷新,web页面不重新加载渲染,在url地址后面加t=时间戳 js脚本 img.src=url+"?t="+Math.random() 如果是jsp页面,要 ...
- 使用jvisualvm远程监控tomcat(阿里云ECS)
写在前面: 使用jvisualvm远程监控tomcat(阿里云ECS),连接是报错:service:jmx:rmi:////jndi/rmi:IP:端口// 连接到 IP:端口,网上找了很多资料, ...