当前位置:网站首页>【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;
}
}
};
边栏推荐
- Feature extraction and detection 16 brisk feature detection and matching
- Docker installing Oracle_ 11g
- Should enterprises choose server free computing?
- error: . repo/manifests/: contains uncommitted changes
- VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
- Circular statements in shell programming
- SAP ui5 beginner tutorial XXI - trial version of custom formatter of SAP ui5
- 电子协会 C语言 1级 32、计算2的幂
- Implementation of Weibo system based on SSM
- 【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
猜你喜欢
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
The role of artificial intelligence in network security
[rust web rokcet Series 1] Hello, world and get, post, put, delete
The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
The technology boss is ready, and the topic of position C is up to you
How can I batch produce the same title for the video?
matlab 实现语音信号重采样和归一化,并播放比对效果
[rust web rokcet Series 2] connect the database and add, delete, modify and check curd
MPLS experiment operation
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
随机推荐
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
Leetcode 45 Jumping game II (2022.02.14)
Android high frequency network interview topic must know and be able to compare Android development environment
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
【图像增强】基于Frangi滤波器实现血管图像增强附matlab代码
[Floyd] post disaster reconstruction
The concept and application of Cartland number
Feature extraction and detection 16 brisk feature detection and matching
k线图形态这样记(口诀篇)
城市选择器组件实现原理
[Chongqing Guangdong education] Tianshui Normal University universe exploration reference
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
游戏思考15:全区全服和分区分服的思考
Learning note 24 - multi sensor post fusion technology
How does schedulerx help users solve the problem of distributed task scheduling?
ES6 new method of string