当前位置:网站首页>【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;
}
}
};
边栏推荐
- Learn C language from scratch day 025 (maze)
- How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
- Learning note 3 -- Key Technologies of high-precision map (Part 1)
- [IVX junior engineer training course 10 papers] 04 canvas and a group photo of IVX and me
- 技术大佬准备就绪,话题C位由你决定
- GL Studio 5 installation and experience
- Basic concepts of machine learning
- KS006基于SSM实现学生成绩管理系统
- Learning note 24 - multi sensor post fusion technology
- 游戏思考15:全区全服和分区分服的思考
猜你喜欢
![[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility](/img/8b/e51867cfe9d200ac385e1d1f01e4b3.jpg)
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility

Basic concepts of machine learning

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

Learn basic K-line diagram knowledge in three minutes

Four basic strategies for migrating cloud computing workloads

matlab 使用 audioread 、 sound 读取和播放 wav 文件

Leetcode, 3 repeatless longest subsequence

GL Studio 5 安装与体验

【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面

Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
随机推荐
Modeling essays series 124 a simple coding method
[Floyd] post disaster reconstruction
Penser au jeu 15: penser au service complet et au sous - service
SAP ui5 beginner tutorial XXI - trial version of custom formatter of SAP ui5
Raspberry pie 4B learning notes - IO communication (1-wire)
Basic usage of shell script
6-2漏洞利用-ftp不可避免的问题
matlab 实现语音信号重采样和归一化,并播放比对效果
Tencent cloud techo youth dream campus trip into Wuhan University
CEPH buffer yyds dry inventory
Single chip microcomputer -- hlk-w801 transplant NES simulator (III)
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model
NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
How does schedulerx help users solve the problem of distributed task scheduling?
只是以消费互联网的方式和方法来落地和实践产业互联网,并不能够带来长久的发展
Self drawing of menu items and CListBox items
[Chongqing Guangdong education] Tianshui Normal University universe exploration reference
[rust web rokcet Series 2] connect the database and add, delete, modify and check curd
uTools