当前位置:网站首页>235. 二叉搜索树的最近公共祖先-dfs法
235. 二叉搜索树的最近公共祖先-dfs法
2022-07-20 23:10:00 【Mr Gao】
235. 二叉搜索树的最近公共祖先
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
示例 1:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6。
示例 2:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
解题代码如下:
很不错的一个题目,感兴趣的,可以学习学习,有几个细节需要我们去细心的处理:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
struct TreeNode* re;
int find;
int dfs(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q){
if(root){
int l=dfs(root->left,p,q);
int r=dfs(root->right,p,q);
if(l==1&&r==1){
re=root;
return 0;
}
if((l==1||r==1)&&(root==p||root==q)){
re=root;
return 0;
}
if(root==p||root==q){
return 1;
}
if(r==1||l==1){
return 1;
}
return 0;
}
else{
return 0;
}
}
// void dfs2(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q){
// if(root){
// dfs2(root->left,p,q);
// dfs2(root->right,p,q);
// if(root==p||root==q){
// find++;
// }
// if(find==2){
// re=root;
// find=0;
// }
// }
// }
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
re=NULL;
dfs(root,p,q);
return re;
}
边栏推荐
- 华为无线设备配置流量监管
- Interview must ask, how to ensure the idempotency of the interface?
- 华为无线设备配置同一业务VLAN的AP间快速漫游
- "Cloud primitives kubernetes" deeply analyzes the principle of pod in kubernetes
- 2022-07-20 第五小组 修身课 学习笔记(every day)
- Configuring airtime scheduling for Huawei wireless devices
- ARIMA疫情期间港口靠挂数的时间序列分析
- Self signed San certificate
- What is a video content recommendation engine?
- It seems to be a bug of thread pool, but I think the source code design is unreasonable.
猜你喜欢
Huawei wireless device roaming configures non fast roaming between APs of the same service VLAN
进程的阻塞和唤醒
[interview: concurrent Article 19: multithreading: Park & unpark]
泄漏电流、无接地保护措施……1296个进口电热水壶存在重大安全隐患
【图文并茂】这次一文讲透JVM架构、类文件结构、字节码结构!!
华为无线设备配置不同业务VLAN的AP间快速漫游
Let me tell you how a grass-roots programmer can counter attack and successfully enter bat!
MD编辑器 - Typora
@RequestParam注解的正确使用方式
二维数组中查找
随机推荐
看起来是线程池的BUG,但是我认为是源码设计不合理。
JS -- basic grammar
如何将服务器上的文件_按自定义的层级关系_进行压缩并下载
16 SQL injection test points you should pay attention to
Latest microservice component selection
The remote control software should also have plan B alternatives
封装全局input组件,并通过v-model绑定父子数据
Power Bi ---- slicer to make the report more beautiful
[pictures and texts] this article explains the JVM architecture, class file structure and bytecode structure!!
When using mysql, please make good use of JSON
华为无线设备漫游配置同一业务VLAN的AP间非快速漫游
It seems to be a bug of thread pool, but I think the source code design is unreasonable.
ES基础知识和常用查询基础
cv demo
华为无线设备配置不同业务VLAN的AP间快速漫游
Ultra simple three tube inductive brushless three-phase motor drive board
JS-添加方式(行内 内嵌 外部)
复杂链表的复制
Huawei wireless devices are configured with non fast roaming between APs of different service VLANs
JS--语法基础