蓝桥杯真题 左儿子右兄弟(图解、代码详解)

题目描述:

对于一棵多叉树,我们可以通过“左孩子右兄弟” 表示法,将其转化成一棵二叉树。
如果我们认为每个结点的子结点是无序的,那么得到的二叉树可能不唯一。
换句话说,每个结点可以选任意子结点作为左孩子,并按任意顺序连接右兄弟。
给定一棵包含N 个结点的多叉树,结点从1 至N 编号,其中1 号结点是根,每个结点的父结点的编号比自己的编号小。
请你计算其通过“左孩子右兄弟” 表示法转化成的二叉树,高度最高是多少。
注:只有根结点这一个结点的树高度为0 。

输入格式:

输入的第一行包含一个整数N。
以下N-1 行,每行包含一个整数,依次表示2 至N 号结点的父结点编号。
对于30% 的评测用例,1 ≤ N ≤ 20;
对于所有评测用例,1 ≤ N ≤ 100000。

输出格式

输出一个整数表示答案

思路:

数据结构中学过孩子兄弟表示法,这里类似,父节点的孩子选择第一个作为左孩子,其他的孩子作为第一个孩子的右孩子依次线性连接,只是题目这里改成了,孩子的顺序可以随意选。也就是不必选择第一个孩子作为父节点的左孩子,这个左孩子可以从孩子当中人选,可以画图,容易得出,我们要尽可能的延展树的深度,必须以父节点的孩子中它的孩子数目最多的孩子结点作为尾结点才能延展得更深,如图下例子,1有三个孩子为2,3,4,而2有一个孩子,我们最优的做法就是将1的孩子中孩子数目最多的结点2作为尾结点,才能继续把结点2看作像结点1一样继续延展下去。所以按照思路,可以直接从结点1开始深搜。

 

 所以易得出 树的最大高度=父节点孩子的数目+以它的孩子为父节点的子树的最大高度(递归定义)

代码如下:

#include<iostream>
#include<vector>
using namespace std;
vector<int> f[100050];//f[i]容器中装的是以i结点为父亲的所有孩子结点
int dfs(int node) {
	int count = 0;//存储每一层的孩子的最大孩子数目
	if (f[node].size() == 0) return 0; //递归出口, 似乎也可以不用这一句,到最后一层 count和f[node].size()都为0
	for (int i = 0; i < f[node].size(); i++) {
		count = max(count, dfs(f[node][i]));//求结点孩子的最深层数
	}
	return count + f[node].size();//递归出口,count为node结点孩子的最大孩子数目,f[node].size()是node这一层兄弟数目
}
int main() {
	int n;//n为结点个数
	int t;//
	cin >> n;
	for (int i = 2; i <= n; i++) {
		cin >> t;
		f[t].push_back(i);// i结点的父亲是x结点  所以利用f[t].size()可以求得t结点的孩子数目
	}
	cout << dfs(1);
	return 0;
}

  • 58
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是对应的代码实现: ```c++ #include <iostream> #include <vector> using namespace std; struct TreeNode{ char val; TreeNode *firstChild; TreeNode *nextSibling; TreeNode(char x) : val(x), firstChild(NULL), nextSibling(NULL) {} }; void buildTree(TreeNode* &root, char fa, char ch){ if(!root){ root = new TreeNode(fa); } TreeNode *p = root; while(p->val != fa){ if(p->nextSibling){ p = p->nextSibling; } else{ p->nextSibling = new TreeNode(ch); return; } } if(!p->firstChild){ p->firstChild = new TreeNode(ch); } else{ p = p->firstChild; while(p->nextSibling){ p = p->nextSibling; } p->nextSibling = new TreeNode(ch); } } void preOrder(TreeNode* root){ if(!root){ return; } cout << root->val << " "; preOrder(root->firstChild); preOrder(root->nextSibling); } void postOrder(TreeNode* root){ if(!root){ return; } postOrder(root->firstChild); postOrder(root->nextSibling); cout << root->val << " "; } int getDepth(TreeNode* root){ if(!root){ return 0; } int maxDepth = 0; TreeNode *p = root->firstChild; while(p){ int depth = getDepth(p); maxDepth = max(maxDepth, depth); p = p->nextSibling; } return maxDepth + 1; } int getLeafCount(TreeNode* root){ if(!root){ return 0; } if(!root->firstChild){ return 1; } int count = 0; TreeNode *p = root->firstChild; while(p){ count += getLeafCount(p); p = p->nextSibling; } return count; } void printPath(TreeNode* root, vector<char>& path){ if(!root){ return; } if(!root->firstChild){ cout << "Path: "; for(char c : path){ cout << c << " "; } cout << root->val << endl; return; } path.push_back(root->val); TreeNode *p = root->firstChild; while(p){ printPath(p, path); p = p->nextSibling; } path.pop_back(); } int main(){ TreeNode* root = NULL; char fa, ch; while(cin >> fa >> ch){ buildTree(root, fa, ch); } cout << "PreOrder: "; preOrder(root); cout << endl; cout << "PostOrder: "; postOrder(root); cout << endl; cout << "Depth: " << getDepth(root) << endl; cout << "Leaf Count: " << getLeafCount(root) << endl; vector<char> path; printPath(root, path); return 0; } ``` 输入格式为: ``` a b a c b d b e c f c g f h f i g j g k ``` 输出结果为: ``` PreOrder: a b d e c f h i g j k PostOrder: e d b h i f j k g c a Depth: 3 Leaf Count: 6 Path: Path: a b d e Path: a b d e Path: a b e Path: a c f h Path: a c f i Path: a c g j Path: a c g k ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值