当前位置:网站首页>Leetcode——236. 二叉树的最近公共祖先
Leetcode——236. 二叉树的最近公共祖先
2022-07-07 12:15:00 【styfish】
概述
分析
题目要求找树中等两个节点的最近公共祖先,容易想到应该从下往上查找,进而想到使用后序遍历
通过后序遍历,可以从树的下面开始遍历一个结点的左右子树,如果左右子树分别有指定的结点的话,则该节点一定是最低的公共祖先
因为后序一定是从下面开始的,如果左右子树中有指定结点的话,那么一定是第一次出现
思路
如何确定左右子树是否拥有指定的结点?
- 遍历过程中,判断遍历的节点是否和指定结点相同,如果相同,则返回该结点
代码
class Solution {
public:
// 4. 确定递归函数返回值
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
// 1. 确定递归函数
// 3. 确定终止条件。 如果节点==p、q则说明找到了;如果节点=nullptr,则遍历结束
if (root == p || root == q || root = nullptr) {
// 如果找到指定的节点 or 为空节点
return root;
}
// 1. 确定单层递归逻辑————后序遍历
TreeNode *lNode=lowestCommonAncestor(root -> left, p, q);
TreeNode *rNode=lowestCommonAncestor(root -> right, p, q);
// 2. 明确递归函数的作用
// lowestCommonAncestor的作用:判断在以root节点为根的子树中,是否拥有p、q指向的结点
if (lNode != nullptr && rNode != nullptr) // 如果在左右子树中,
return root;
else if(lNode == nullptr) {
//两个都在右子树
return rNode;
}
else {
//两个都在左子树里面
return lNode;
}
return nullptr;
}
};
边栏推荐
- The difference between memory overflow and memory leak
- PERT图(工程网络图)
- The meaning of variables starting with underscores in PHP
- Transferring files between VMware and host
- 566. Reshaping the matrix
- Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
- Codes de non - retour à zéro inversés, codes Manchester et codes Manchester différentiels couramment utilisés pour le codage des signaux numériques
- Huawei image address
- Laravel Form-builder使用
- 【网络安全】sql注入语法汇总
猜你喜欢
一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
Details of redis core data structure & new features of redis 6
Vscode configuration uses pylint syntax checker
Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
AI talent cultivation new ideas, this live broadcast has what you care about
高等數學---第八章多元函數微分學1
SAKT方法部分介绍
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
高等数学---第八章多元函数微分学1
. Net core about redis pipeline and transactions
随机推荐
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
UML 状态图
2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
Supply chain supply and demand estimation - [time series]
ARM Cortex-A9,MCIMX6U7CVM08AD 处理器应用
Excuse me, I have three partitions in Kafka, and the flinksql task has written the join operation. How can I give the join operation alone
What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?
GVIM [III] [u vimrc configuration]
MySQL "invalid use of null value" solution
Oracle advanced (V) schema solution
2022-7-6 beginner redis (I) download, install and run redis under Linux
杭电oj2092 整数解
Take you to master the three-tier architecture (recommended Collection)
数据流图,数据字典
TPG x AIDU | AI leading talent recruitment plan in progress!
Parsing of XML files
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
Realization of search box effect [daily question]
gvim【三】【_vimrc配置】