当前位置:网站首页>【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;
}
}
};
边栏推荐
- [rust web rokcet Series 1] Hello, world and get, post, put, delete
- NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
- [IVX junior engineer training course 10 papers] 04 canvas and a group photo of IVX and me
- 人工智能在网络安全中的作用
- 浅浅了解Servlet
- matlab 使用 audioread 、 sound 读取和播放 wav 文件
- No converter found for return value of type: class
- 技术大佬准备就绪,话题C位由你决定
- matlab 实现语音信号重采样和归一化,并播放比对效果
- Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
猜你喜欢

matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音

Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated

Raspberry pie 4B learning notes - IO communication (1-wire)

Data visualization in medical and healthcare applications

Should enterprises choose server free computing?

【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
![[IVX junior engineer training course 10 papers to get certificates] 09 chat room production](/img/a8/25215e74162b7ab3f29df65681932b.jpg)
[IVX junior engineer training course 10 papers to get certificates] 09 chat room production

Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories

Introduction to ffmpeg Lib

Appium inspector can directly locate the WebView page. Does anyone know the principle
随机推荐
MPLS experiment operation
Four basic strategies for migrating cloud computing workloads
I Brief introduction of radio energy transmission technology
Basic number theory -- Gauss elimination
学习笔记24--多传感器后融合技术
GL Studio 5 installation and experience
No converter found for return value of type: class
三分钟学会基础k线图知识
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
uTools
We should make clear the branch prediction
10 minutes to get started quickly composition API (setup syntax sugar writing method)
[IVX junior engineer training course 10 papers to get certificates] 0708 news page production
Hcip day 14 (MPLS protocol)
6-2漏洞利用-ftp不可避免的问题
站在新的角度来看待产业互联网,并且去寻求产业互联网的正确方式和方法
About asp Net core uses a small detail of datetime date type parameter
Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
现货黄金分析的技巧有什么呢?
【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面