当前位置:网站首页>The nearest common ancestor of binary tree
The nearest common ancestor of binary tree
2022-06-11 18:28:00 【HHYX.】
The nearest common ancestor of a binary tree
Topic link : The nearest common ancestor of a binary tree
Title Description
Given a binary tree , Find the nearest common ancestor of the two specified nodes in the tree .
In Baidu Encyclopedia, 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 ).”
Topic analysis
For a binary tree , There are many ways to find the nearest common ancestor node , Only one of them is introduced here . According to the meaning , To find the nearest public ancestor node , First, for a node , If p and q In the left and right subtrees of this node , So the node is pq The nearest common ancestor node of . If both nodes are in the left or right subtree of a node , Then you can recursively go to the next node for judgment , Note that a node may be its own ancestor node , namely p Nodes may also be q The ancestor node of the node , Situation judgment is required . The code implementation is as follows :
Code implementation
bool Find(TreeNode* root, TreeNode* key)// lookup key Whether it exists in the subtree of this node
{
if (root == nullptr)
{
return false;
}
if (root == key)
{
return true;
}
return Find(root->left, key) || Find(root->right, key);
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
{
bool leftp = Find(root->left, p);
bool rightp = Find(root->right, p);
bool leftq = Find(root->left, q);
bool rightq = Find(root->right, q);
if (root == p || root == q)
{
return root;
}
else if ((leftp && rightq)||(rightp && leftq))//p and q Respectively in root On the left and right subtrees of
{
return root;
}
else if ((leftp && leftq))//p and q On the same left
{
return lowestCommonAncestor(root->left, p, q);
}
else if (rightp && rightq)
{
return lowestCommonAncestor(root->right, p, q);
}
else
{
return nullptr;
}
}

边栏推荐
- DC-DC自举电容(BOOT)几个问题
- 5分钟了解攻防演练中的红蓝紫
- .net core redis hyperloglog类型
- Explain AI accelerators in detail: GPU, DPU, IPU, TPU... There are infinite possibilities for AI acceleration schemes
- Talking about telework | community essay solicitation
- 为何TI的GPMC并口,更常被用于连接FPGA、ADC?我给出3个理由
- Force buckle 31 next arrangement
- 排序的循环链表
- MMA-Self-defining function
- 论催收系统的管理子系统选型设计
猜你喜欢
随机推荐
[C语言]用结构体把平均分和低于等于平均分的学生数据输出
Ctfhub SQL Boolean blind annotation
[C语言]用结构体把最高分的学生输出,可有多个最高分
[C语言]限制查找次数,输出次数内查找到的最大值
力扣23题,合并K个升序链表
PIL-Pillow图像处理【1】-安装与新建
金融银行_催收系统简介
合并多棵二叉搜索树
v-for循环遍历
全志T3开发板(4核ARM Cortex-A7)——系统启动阶段LOGO显示详解
New work of "the father of LSTM": a new method towards self correcting neural network
5 minutes to understand the red, blue and purple in the attack and defense drill
Learn to use LSTM and IMDB comment data for emotion analysis training
[c language] compress strings and add markup characters
Two methods for matlab to save imshow drawing pictures to a specified folder
How to learn and self-study
Cryptology Summary
全志科技T3开发板(4核ARM Cortex-A7)——MQTT通信协议案例
vim常用命令
Feign shares login information for request


![[C语言]限制查找次数,输出次数内查找到的最大值](/img/e6/cbb8dd54b49ade453251a70c8455e8.png)
![[c language] output the average score and the student data below or equal to the average score with the structure](/img/c4/263301a22b61c86a3e0df6ad2596f1.png)





