当前位置:网站首页>寻找二叉树中任意两个数的公共祖先
寻找二叉树中任意两个数的公共祖先
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);
}

边栏推荐
- Leetcode922 sort array by parity II
- Orb-slam2 data sharing and transmission between different threads
- Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
- [geek challenge 2019] upload
- Codeforces 771 div2 B (no one FST, refers to himself)
- Depth filter of SvO2 series
- 测试左移和右移
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
- Map和Set
猜你喜欢

From scratch, develop a web office suite (3): mouse events

How to Add P-Values onto Horizontal GGPLOTS

CONDA common command summary

自然语言处理系列(三)——LSTM

记录一下MySql update会锁定哪些范围的数据

MSI announced that its motherboard products will cancel all paper accessories

倍增 LCA(最近公共祖先)

xss-labs-master靶场环境搭建与1-6关解题思路

甜心教主:王心凌

Heap (priority queue)
随机推荐
小程序链接生成
排序---
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
[C language] Yang Hui triangle, customize the number of lines of the triangle
Mish-撼动深度学习ReLU激活函数的新继任者
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
Dynamic memory (advanced 4)
Gaode map test case
机械臂速成小指南(七):机械臂位姿的描述方法
CDA数据分析——AARRR增长模型的介绍、使用
jenkins 凭证管理
Natural language processing series (I) -- RNN Foundation
CONDA common command summary
YYGH-BUG-05
YYGH-BUG-04
mysql表的增删改查(进阶)
WSL 2 will not be installed yet? It's enough to read this article
Leetcode14 longest public prefix
Uniapp uni list item @click, uniapp uni list item jump with parameters
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)