当前位置:网站首页>【LeetCode 43】236. The nearest common ancestor of binary tree
【LeetCode 43】236. The nearest common ancestor of binary tree
2022-07-02 01:34:00 【CodeLinghu】
【LeetCode 43】236. The nearest common ancestor of a binary tree
List of articles
One 、 The question

Two 、 The answer process
** Ideas / Method :** This question uses bottom-up search ------ to flash back ! Recursion is also used !
If you find a node , It is found that there are nodes in the left subtree p, A node appears in the right subtree q, perhaps Nodes appear in the left subtree q, A node appears in the right subtree p, So the node is the node p and q The latest public ancestor of .
Use After the sequence traversal , Backtracking process , It is to traverse nodes from low to high , Once the node of this condition is found , It's the nearest public node .

- First find the node path p and q
- At the nearest common ancestor node of the node path

Logical processing in recursive trilogy :
//
//3. Traverse the whole tree
TreeNode *left=lowestCommonAncestor(root->left,p,q);
TreeNode *right=lowestCommonAncestor(root->right,p,q);
When a recursive function has a return value :
- Search for an edge :
if ( Recursive function (root->left)) return ;
if ( Recursive function (root->right)) return ;
- Search the whole tree :
left = Recursive function (root->left);
right = Recursive function (root->right);
left And right Logical processing of ;
When a recursive function has a return value : If you want to search for an edge , When the return value of recursive function is not empty , Go back to , If you search the whole tree , Just use a variable left、right Catch the return value , This left、right There is also the need for logic processing in the post order , That is to say, the logic to deal with intermediate nodes in post order traversal ( It's also retrospective )
class Solution {
public://1.
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
//2. If a node is found p Or nodes q Or an empty node is encountered . Just go back to
if(root==q||root==p||root==NULL) return root;
//3. Traverse the whole tree
TreeNode *left=lowestCommonAncestor(root->left,p,q);// Constantly traverse the left subtree
TreeNode *right=lowestCommonAncestor(root->right,p,q);// Constantly traverse the right subtree
// Discussion :
if(left!=NULL&&right!=NULL) return root;
if(left==NULL&&right!=NULL) return right;
else if(left!=NULL&&right==NULL) return left;
else{
//left==NULL&&right==NULL
return NULL;
}
}
};
边栏推荐
- II Basic structure of radio energy transmission system
- 站在新的角度来看待产业互联网,并且去寻求产业互联网的正确方式和方法
- GL Studio 5 installation and experience
- Have you stepped on the nine common pits in the e-commerce system?
- Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
- [IVX junior engineer training course 10 papers] 06 database and services
- [IVX junior engineer training course 10 papers to get certificates] 0708 news page production
- Liteos learning - first knowledge of development environment
- Sql--- related transactions
- 学习笔记25--多传感器前融合技术
猜你喜欢

What are the skills of spot gold analysis?

Liteos learning - first knowledge of development environment

基于SSM实现微博系统

Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages

The role of artificial intelligence in network security

【图像增强】基于Frangi滤波器实现血管图像增强附matlab代码

Memorabilia of domestic database in June 2022

Learning notes 25 - multi sensor front fusion technology
![[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production](/img/dc/e9adb1b41c2175c6f18d8027e0530a.jpg)
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production

MPLS experiment operation
随机推荐
LeetCode、3无重复最长子序列
Memorabilia of domestic database in June 2022
No converter found for return value of type: class
Tencent cloud techo youth dream campus trip into Wuhan University
NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production
The author is more willing to regard industrial Internet as a concept much richer than consumer Internet
Ks006 student achievement management system based on SSM
MySQL winter vacation self-study 2022 12 (4)
电商系统中常见的9大坑,你踩过没?
Learn basic K-line diagram knowledge in three minutes
浅浅了解Servlet
Load and domcontentloaded in JS
Sql--- related transactions
Introduction to ffmpeg Lib
ES6 new method of string
MPLS experiment operation
Ubuntu20.04 PostgreSQL 14 installation configuration record
学习笔记3--高精度地图关键技术(上)
学习笔记2--高精度地图定义及价值