3555. 二叉树(北京邮电大学考研机试题)

3555. 二叉树

⭐️难度:简单(其实困难)

⭐️类型:树

📖题目:题目链接

输入样例:

1

8 4

2 3

4 5

6 -1

-1 -1

-1 7

-1 -1

8 -1

-1 -1

1 6

4 6

4 5

8 1

输出样例:

2

4

2

4

🌟思路:

先试着把样例中的树画出来:

再思考怎么找两个结点的最小距离。

第一种情况:2和8的距离,2已经在8的路径上了,很容易算得2到8的距离;

第二种情况:4和8的距离,4和8不在同一条路径上,此时需要找4和8路径共同的交点 ,也就是2,再计算4到2的距离+8到2的距离

步骤:

1️⃣先构造树

2️⃣找到结点到根的路径

3️⃣找交点

4️⃣总结规律

笔记:

📚题解:

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<vector>  // vector不需要.h
#include<list>
#include<set>  // // 可以用 set 和 multiset
#include<unordered_set> // 可以用 unordered_set 和 unordered_multiset
#include<map>  // 可以用 map 和 multimap
#include<unordered_map> // 可以用 unordered_map 和 unordered_multimap
#include<algorithm>
#include<string>
#include<iostream>
#include<queue>
#include<stack>

using namespace std;


struct TreeNode {
    int num;
    TreeNode* left; 
    TreeNode* right;
    TreeNode* parent;  // 因为要沿上找路径,定义parent会更方便
};

int main() {
 
    int t;
    scanf("%d", &t);
    for (int i = 0;i < t;i++) {
        int n, m;
        scanf("%d%d", &n, &m);

        vector<TreeNode*> nodearr(n + 1);  // 定义n+1空间,方便与结点编号对应
        for (int j = 1;j <= n;j++) {
            nodearr[j] = new TreeNode;
            nodearr[j]->num = j;
        }

        nodearr[1]->parent = NULL;

        // 构建树
        for (int j = 1;j <= n;j++) {  // 输入n组数据
            int left,right;
            scanf("%d%d", &left, &right);
            if (left != -1) {
                nodearr[j]->left = nodearr[left];   // 第i行输入表示第i个结点左右孩子的情况
                nodearr[left]->parent = nodearr[j];
            }
            else {
                nodearr[j]->left = NULL;
            }
            if (right != -1) {
                nodearr[j]->right = nodearr[right];
                nodearr[right]->parent = nodearr[j];
            }
            else {
                nodearr[j]->right = NULL;
            }
        }

        int lhs, rhs;
        for (int j = 0;j < m;j++) {  // 输入m组待测数据
            scanf("%d%d", &lhs, &rhs);
            vector<int> lvec;  // 存所给第一个结点的路径
            TreeNode* p = nodearr[lhs];
            while (p != NULL) {
                lvec.push_back(p->num);
                p = p->parent;
            }
            vector<int> rvec;  // 存所给第二个结点的路径
            p = nodearr[rhs];
            while (p != NULL) {
                rvec.push_back(p->num);
                p = p->parent;
            }

            // 找交点
            int l = lvec.size() - 1;
            int r = rvec.size() - 1;
            while (true) {
                if (l < 0 || r < 0|| (lvec[l] != rvec[r])) {  // 没有找到公共点 或 左结点和右节点到公共交点前一个结点
                    break;
                }
                l--;
                r--;
            }
            printf("%d\n", l + r + 2);
        }

    }

    

    return 0;
}
相关推荐
LUVK_19 小时前
第七章查找
数据结构·c++·考研·算法·408
做cv的小昊1 天前
【TJU】研究生应用统计学课程笔记(4)——第二章 参数估计(2.1 矩估计和极大似然估计、2.2估计量的优良性原则)
人工智能·笔记·考研·数学建模·数据分析·excel·概率论
王_teacher3 天前
机器学习 矩阵求导 完整公式+严谨推导
人工智能·线性代数·考研·机器学习·矩阵·线性回归
求学的小高3 天前
数据结构Day6(普通树、森林与二叉树的关系、哈夫曼编码、并查集)
数据结构·笔记·考研
米码收割机5 天前
【考研】2025 沈阳工业大学827自动控制原理考研真题及答案
考研
Java_小白呀5 天前
考研408数据结构(栈与队列)
数据结构·考研·栈和队列·考研408
imbackneverdie7 天前
科研绘图踩坑多年,我总结出了零设计基础出期刊级插图的方法
人工智能·考研·ai·信息可视化·ai作画·科研绘图·研究生
我是无敌小恐龙7 天前
线下班第一课
python·考研·django·ai编程
codebrick7 天前
408 数据结构:快排 / 堆排 / 归并 / 希尔 等排序算法对比(复杂度、稳定性、真题考点
数据结构·考研·算法·排序算法·408
MaCa .BaKa7 天前
52-考研备考服务平台系统-考研系统
java·spring boot·mysql·考研·tomcat·maven·mybatis