当前位置:网站首页>Leetcode——236. The nearest common ancestor of binary tree
Leetcode——236. The nearest common ancestor of binary tree
2022-07-07 14:18:00 【styfish】
summary
236. The nearest common ancestor of a binary tree
analysis
The problem requires finding the nearest common ancestor of two nodes in the tree , It's easy to think of finding it from the bottom up , Then I think of using post order traversal
Through post order traversal , You can traverse the left and right subtrees of a node from the bottom of the tree , If the left and right subtrees have specified nodes respectively , Then the node must be the lowest common ancestor
Because the sequence must start from the following , If there are specified nodes in the left and right subtrees , Then it must be the first time
Ideas
How to determine whether the left and right subtrees have specified nodes ?
- During traversal , Judge whether the traversal node is the same as the specified node , If the same , The node is returned
Code
class Solution {
public:
// 4. Determine the return value of recursive function
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
// 1. Determine the recursive function
// 3. Determine termination conditions . If node ==p、q It says it found it ; If node =nullptr, Then the traversal ends
if (root == p || root == q || root = nullptr) {
// If the specified node is found or Is an empty node
return root;
}
// 1. Determine single level recursive logic ———— After the sequence traversal
TreeNode *lNode=lowestCommonAncestor(root -> left, p, q);
TreeNode *rNode=lowestCommonAncestor(root -> right, p, q);
// 2. Clarify the role of recursive functions
// lowestCommonAncestor The role of : Judgment is based on root In the subtree whose node is the root , Owned or not p、q Pointed node
if (lNode != nullptr && rNode != nullptr) // If in the left and right subtrees ,
return root;
else if(lNode == nullptr) {
// Both are in the right subtree
return rNode;
}
else {
// Both are in the left subtree
return lNode;
}
return nullptr;
}
};
边栏推荐
- Lavarel之环境配置 .env
- Csma/cd carrier monitoring multipoint access / collision detection protocol
- 2022-7-7 Leetcode 844. Compare strings with backspace
- Huawei image address
- Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
- Similarities and differences between switches and routers
- How to check the ram and ROM usage of MCU through Keil
- 搜索引擎接口
- UML sequence diagram (sequence diagram)
- VSCode 配置使用 PyLint 语法检查器
猜你喜欢

Hands on Teaching: XML modeling

The longest ascending subsequence model acwing 1012 Sister cities

Take you to master the three-tier architecture (recommended Collection)

Parsing of XML files

UML state diagram

常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼

最长上升子序列模型 AcWing 1014. 登山

最长上升子序列模型 AcWing 1012. 友好城市

带你掌握三层架构(建议收藏)

Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
随机推荐
Battle Atlas: 12 scenarios detailing the requirements for container safety construction
一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
3D Detection: 3D Box和点云 快速可视化
ARM Cortex-A9,MCIMX6U7CVM08AD 处理器应用
Introduction to sakt method
UML state diagram
Supply chain supply and demand estimation - [time series]
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
[Reading stereo matching papers] [III] ints
Selenium Library
Vmware 与主机之间传输文件
The meaning of variables starting with underscores in PHP
高等数学---第八章多元函数微分学1
Es log error appreciation -limit of total fields
c#通过frame 和 page 切换页面
[untitled]
[AI practice] Application xgboost Xgbregressor builds air quality prediction model (II)
Cesium 已知一点经纬度和距离求另一个点的经纬度
Did login metamask
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?