当前位置:网站首页>【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;
}
}
};
边栏推荐
- Ubuntu20.04 PostgreSQL 14 installation configuration record
- error: . repo/manifests/: contains uncommitted changes
- [Maya] the error of importing Maya into Metahuman
- MySQL application day02
- [rust web rokcet Series 1] Hello, world and get, post, put, delete
- MySQL winter vacation self-study 2022 12 (4)
- Feature extraction and detection 16 brisk feature detection and matching
- I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)
- [IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production
- Data visualization in medical and healthcare applications
猜你喜欢
游戏思考15:全区全服和分区分服的思考
Leetcode, 3 repeatless longest subsequence
Have you stepped on the nine common pits in the e-commerce system?
人工智能在网络安全中的作用
Should enterprises choose server free computing?
Four basic strategies for migrating cloud computing workloads
Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model
遊戲思考15:全區全服和分區分服的思考
Exclusive delivery of secret script move disassembly (the first time)
Matlab uses audioread and sound to read and play WAV files
随机推荐
A problem about function template specialization
How does schedulerx help users solve the problem of distributed task scheduling?
城市选择器组件实现原理
迁移云计算工作负载的四个基本策略
人工智能在网络安全中的作用
Architecture evolution from MVC to DDD
Edge extraction edges based on Halcon learning_ image. Hdev routine
About asp Net core uses a small detail of datetime date type parameter
Day 13 of hcip (relevant contents of BGP agreement)
Ks006 student achievement management system based on SSM
Principle of finding combinatorial number and template code
ES6 new method of string
Since I understand the idea of dynamic planning, I have opened the door to a new world
Brief description of grafana of # yyds dry goods inventory # Prometheus
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
关于ASP.NET CORE使用DateTime日期类型参数的一个小细节
Docker installing Oracle_ 11g
Learning note 24 - multi sensor post fusion technology
Introduction to ffmpeg Lib
ECS project deployment