当前位置:网站首页>The nearest common ancestor of binary (search) tree ●●
The nearest common ancestor of binary (search) tree ●●
2022-07-06 22:13:00 【chenyfan_】
236. The nearest common ancestor of a binary tree ●●
describe
Given a binary tree , Find two specified nodes in the tree Recent public ancestor .
The most recent definition of public ancestor is :“ For a tree T Two nodes of p、q, The nearest common ancestor is represented as a node x, Satisfy x yes p、q Our ancestors and x As deep as possible ( A node can also be its own ancestor ).”
Example
Input :root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output :5
explain : node 5 And nodes 4 The most recent public ancestor of is the node 5 . Because by definition, the nearest common ancestor node can be the node itself .
Answer key
1. After the sequence traversal ( to flash back )
The first thing you can easily think of : 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 .
But there is a second situation , Is the node itself p(q), It has a grandchild node q§.
Use post order traversal , The process of backtracking , Namely Traverse nodes from low to high , Once the node satisfying the first condition is found , This is the nearest public node .
But if p perhaps q Itself is the nearest public ancestor ?
Actually Just find a node that is p perhaps q When , Return the current node directly , There is no need to continue recursive subtrees . If the next ( Up ) The following node is found in the traversal ( Parent node ) If the first condition is met, the return value is modified to the subsequent node , otherwise , Continue to return to the found node .
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 ( Recursive function (root->left)) return ;
if ( Recursive function (root->right)) return ;
If you want to 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 ).
left = Recursive function (root->left);
right = Recursive function (root->right);
left And right Logical processing of ;
- Time complexity O(N) : among N Is the number of binary tree nodes ; In the worst case , You need to recursively traverse all the nodes of the tree .
- Spatial complexity O(N) : In the worst case , The depth of recursion reaches N , System use O(N) The size of the extra space .
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == q || root == p || root == NULL) return root; // Judge the current node
TreeNode* left = lowestCommonAncestor(root->left, p, q); // Traverse left subtree
TreeNode* right = lowestCommonAncestor(root->right, p, q); // Traversal right subtree
if(left != NULL && right != NULL){
return root; // Both left and right subtrees have return values , Then the current is the nearest common ancestor
}else if(left != NULL){
return left; // Only the left subtree has a return value
}else if(right != NULL){
return right; // Only the right subtree has a return value
}
return NULL;
}
};
235. The nearest common ancestor of a binary search tree ●
describe
Given a binary search tree , Find two specified nodes in the tree Recent public ancestor .
The most recent definition of public ancestor is :“ For a tree T Two nodes of p、q, The nearest common ancestor is represented as a node x, Satisfy x yes p、q Our ancestors and x As deep as possible ( A node can also be its own ancestor ).”
Example
p = 4, q = 7; Output : 6
p = 2, q = 4; Output : 2
Answer key
Different from ordinary binary tree , Binary search trees are ordered , as long as Traverse from top to bottom When ,cur The node is the value in [p, q] In interval It indicates that the node cur It's the recent public ancestor , And you don't need to traverse the whole tree , Find the result and return directly .
1. Sequence traversal queue iteration
The search direction is not adjusted according to the size of the node , Low efficiency .
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
int Min = min(p->val, q->val);
int Max = max(p->val, q->val);
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
TreeNode* curr = que.front();
que.pop();
if(curr->val <= Max && curr->val >= Min) return curr;
if(curr->left) que.push(curr->left);
if(curr->right) que.push(curr->right);
}
return NULL;
}
};
2. The former sequence traversal
Judge and adjust the search direction according to the size of the node .
When the node value is greater than p、q When the value of , Search to the left subtree ;
When the node value is less than p、q When the value of , Search the right subtree ;
Otherwise, the node value is in the interval , Meet the conditions , Go straight back to .
recursive
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == NULL) return NULL;
if(root->val > p->val && root->val > q->val){
// in
TreeNode* left = lowestCommonAncestor(root->left, p, q); // Left
if(left != NULL) return left;
}
if(root->val < p->val && root->val < q->val){
TreeNode* right = lowestCommonAncestor(root->right, p, q); // Right
if(right != NULL) return right;
}
return root; // Node value is within the range
}
};
iteration
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
while(root != NULL){
if(root->val > p->val && root->val > q->val){
root = root->left; // To the left
}else if(root->val < p->val && root->val < q->val){
root = root->right; // To the right
}else return root; // Node value is within the range
}
return NULL;
}
};
边栏推荐
- 20 large visual screens that are highly praised by the boss, with source code templates!
- 2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
- Yyds dry goods inventory C language recursive implementation of Hanoi Tower
- What a new company needs to practice and pay attention to
- 基于LM317的可调直流电源
- GPS从入门到放弃(二十)、天线偏移
- GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)
- Oracle-控制文件及日志文件的管理
- 中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
- CCNA-思科网络 EIGRP协议
猜你喜欢
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
GPS从入门到放弃(十三)、接收机自主完好性监测(RAIM)
The golden age of the U.S. technology industry has ended, and there have been constant lamentations about chip sales and 30000 layoffs
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
[daily] win10 system setting computer never sleeps
GPS from getting started to giving up (12), Doppler constant speed
LeetCode:1189. The maximum number of "balloons" -- simple
Make menuconfig has a recipe for target 'menuconfig' failed error
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
Management background --3, modify classification
随机推荐
GPS from entry to abandonment (XVII), tropospheric delay
【sdx62】WCN685X将bdwlan.bin和bdwlan.txt相互转化操作方法
GPS从入门到放弃(十四)、电离层延时
GPS from getting started to giving up (12), Doppler constant speed
AI 企业多云存储架构实践 | 深势科技分享
解决项目跨域问题
Oracle control file and log file management
[asp.net core] set the format of Web API response data -- formatfilter feature
lora同步字设置
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
关于程序员的职业操守,从《匠艺整洁之道》谈起
设置状态栏样式Demo
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
中国VOCs催化剂行业研究与投资战略报告(2022版)
【sciter】: 基于 sciter 封装通知栏组件
Record the process of cleaning up mining viruses
C#實現水晶報錶綁定數據並實現打印4-條形碼
Method return value considerations
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
Management background --3, modify classification