当前位置:网站首页>【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;
}
}
};
边栏推荐
- Study note 2 -- definition and value of high-precision map
- What are the skills of spot gold analysis?
- Learning note 3 -- Key Technologies of high-precision map (Part 1)
- Unity AssetBundle subcontracting
- Android high frequency network interview topic must know and be able to compare Android development environment
- MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
- Three core problems of concurrent programming
- [Floyd] post disaster reconstruction
- We should make clear the branch prediction
- matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
猜你喜欢
Memorabilia of domestic database in June 2022
How does schedulerx help users solve the problem of distributed task scheduling?
学习笔记25--多传感器前融合技术
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
三分钟学会基础k线图知识
Learning note 24 - multi sensor post fusion technology
Should enterprises choose server free computing?
This is the form of the K-line diagram (pithy formula)
About asp Net core uses a small detail of datetime date type parameter
[rust web rokcet Series 1] Hello, world and get, post, put, delete
随机推荐
学习笔记3--高精度地图关键技术(上)
【图像增强】基于Frangi滤波器实现血管图像增强附matlab代码
No converter found for return value of type: class
Variables and constants of go language foundation
[IVX junior engineer training course 10 papers to get certificates] 09 chat room production
学习笔记24--多传感器后融合技术
We should make clear the branch prediction
Feature extraction and detection 16 brisk feature detection and matching
Edge computing accelerates live video scenes: clearer, smoother, and more real-time
6-3 vulnerability exploitation SSH environment construction
Study note 2 -- definition and value of high-precision map
关于ASP.NET CORE使用DateTime日期类型参数的一个小细节
NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
MPLS experiment operation
The author is more willing to regard industrial Internet as a concept much richer than consumer Internet
Implementation of Weibo system based on SSM
Laravel artisan 常用命令
Look at the industrial Internet from a new perspective and seek the correct ways and methods of industrial Internet
Learn about servlets
学习笔记2--高精度地图定义及价值