当前位置:网站首页>寻找二叉树中任意两个数的公共祖先
寻找二叉树中任意两个数的公共祖先
2022-07-02 09:43:00 【weixin_50179990】
代码如下:
void Anales(int arr[],int p, int q)
{
int pos = -1;
int size = 0;
int as[1] = { 0 };
int indexP = 0;
int indexQ = 0;
while (arr[size] != as[1])
{
if (arr[size] == p)
{
indexP = size;
}
if (arr[size] == q)
{
indexQ = size;
}
size++;
}
if (indexP > indexQ)
{
int subInt = indexP - indexQ; //如索引的间隔正好等于更小的那个索引,那么它们的公//共祖先索引为0
if (subInt == indexQ)
{
pos = 0;
}
else
{
int fatherP = indexP;
while (fatherP > indexQ)
{
if (fatherP % 2) //如果当前索引为偶数,它的上一级 = (当前索引 - 2)/2
{
fatherP = (fatherP - 2) / 2;
}
else //如果当前索引为奇数,它的上一级 = (当前索引 - 1)/2
{
fatherP = (fatherP - 1) / 2;
}
}
if (fatherP == indexQ)
{
pos = fatherP;
}
else
{
//同时寻找上级直到相等
while (fatherP != indexQ)
{
if (fatherP % 2)
{
fatherP = (fatherP - 2) / 2;
}
else
{
fatherP = (fatherP - 1) / 2;
}
if (indexQ % 2)
{
indexQ = (indexQ - 2) / 2;
}
else
{
indexQ = (indexQ - 1) / 2;
}
}
pos = indexQ;
}
}
}
else if (indexQ > indexP)
{
int subInt = indexQ - indexP;
if (subInt == indexP)
{
pos = 0;
}
else
{
while (indexQ > indexP)
{
if (indexQ % 2)
{
indexQ = (indexQ - 2) / 2;
}
else
{
indexQ = (indexQ - 1) / 2;
}
}
if (indexP == indexQ)
{
pos = indexQ;
}
else
{
while (indexP != indexQ)
{
if (indexP % 2)
{
indexP = (indexP - 2) / 2;
}
else
{
indexP = (indexP - 1) / 2;
}
if (indexQ % 2)
{
indexQ = (indexQ - 2) / 2;
}
else
{
indexQ = (indexQ - 1) / 2;
}
}
pos = indexQ;
}
}
}
else
{
pos = indexP;
}
std::cout << "公共祖先:" << arr[pos] << "\t 索引:" << pos << std::endl;
}
int main()
{
int arr[15] = { 3,5,1,6,2,0,8,0,0,7,4,0,0,0,0 };
Anales(arr,5,4);
}

边栏推荐
- [geek challenge 2019] upload
- (C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
- MSI announced that its motherboard products will cancel all paper accessories
- PyTorch中repeat、tile与repeat_interleave的区别
- SCM power supply
- lombok常用注解
- Codeforces 771 div2 B (no one FST, refers to himself)
- CDA数据分析——AARRR增长模型的介绍、使用
- How does Premiere (PR) import the preset mogrt template?
- Natural language processing series (III) -- LSTM
猜你喜欢

Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)

Map and set

How to Add P-Values onto Horizontal GGPLOTS

H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开

深入理解PyTorch中的nn.Embedding

YYGH-BUG-04

BEAUTIFUL GGPLOT VENN DIAGRAM WITH R

ES集群中节点与分片的区别

File operation (detailed!)

Natural language processing series (II) -- building character level language model using RNN
随机推荐
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
Flesh-dect (media 2021) -- a viewpoint of material decomposition
Leetcode922 sort array by parity II
jenkins 凭证管理
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
机械臂速成小指南(七):机械臂位姿的描述方法
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
CONDA common command summary
【C语言】十进制数转换成二进制数
How does Premiere (PR) import the preset mogrt template?
Applet link generation
drools执行String规则或执行某个规则文件
Maximum profit of jz63 shares
ES集群中节点与分片的区别
CDH6之Sqoop添加数据库驱动
Differences between nodes and sharding in ES cluster
From scratch, develop a web office suite (3): mouse events
(C语言)八进制转换十进制
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
How to Create a Beautiful Plots in R with Summary Statistics Labels