求改错
  • 板块题目总版
  • 楼主SANHY
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/4/15 22:45
  • 上次更新2023/10/28 03:39:31
查看原帖
求改错
368975
SANHY楼主2022/4/15 22:45
#include <bits/stdc++.h>
#pragma GCC optimize ("2")
using namespace std;
struct node{
	int l, r, w;
}; 
node tree[10001], tree1[10001];
int ma[10001], mm[10001];
int n,m, t=0, tt=0;
void inorder(int idx)	//中序遍历根节点 
{
	if(idx==-1) return;
	inorder(tree[idx].l);	//遍历左子树 
	t++;
	ma[t] = tree[idx].w;
	inorder(tree[idx].r);	//遍历右子树 
}
void inorder1(int idx)	//中序遍历根节点 
{
	if(idx==-1) return;
	inorder1(tree1[idx].l);	//遍历左子树 
	tt++;
	mm[tt] = tree1[idx].w;
	inorder1(tree1[idx].r);	//遍历右子树 
}
int main()
{
	cin >>n;
	for(int i=1; i<=n; i++)
		cin >> tree[i].l >> tree[i].r;
	for(int i=1; i<=n; i++)
		cin >> tree[i].w;
	inorder(1);
	cin >>m;
	for(int i=1; i<=m; i++)
		cin >> tree1[i].l >> tree1[i].r;
	for(int i=1; i<=m; i++)
		cin >> tree1[i].w;
	inorder1(1);
	if(t!=tt)
	{
		cout << "false";
		return 0;
	}
//	for(int i=1; i<=t; i++)	cout << ma[i] << ' ';
//	cout << endl;
//	for(int i=1; i<=tt; i++)	cout << mm[i] << ' ';
	if(t==tt)
	{
		for(int i=1; i<=t; i++)
		{
			if(ma[i]!=mm[i])
			{
				cout << "false";
				return 0;
			}
		}
		
	}
	cout <<"true";
	return 0;
}
题目描述 Description
一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个叶值序列。
image.png
举个例子,如上图所示,给定一棵叶值序列为 (6, 7, 4, 9, 8) 的树。

如果有两棵二叉树的叶值序列是相同,那么我们就认为它们是叶相似的。

如果给定的两个根结点分别为 root1 和 root2 的树是叶相似的,则输出 true;否则输出 false 。

输入描述 Input Description
每个输入数据包含两组二叉树
每组二叉树
第一行一个整数n,表示结点个数
接下来n行,第i行两个空格隔开的整数a b,表示i号结点的左右孩子的编号,如果为编号-1,表示没有左(右)孩子
最后一行,n个空格隔开的整数,第i个整数表示第i个结点的权值

输出描述 Output Description
如果叶相似,输出true;否则输出false

样例输入 Sample Input
【样例1】
9
2 3
4 5
6 7
-1 -1
8 9
-1 -1
-1 -1
-1 -1
-1 -1
3 5 1 6 2 9 8 7 4
9
2 3
4 5
6 7
-1 -1
-1 -1
-1 -1
8 9
-1 -1
-1 -1
3 5 1 6 7 4 2 9 8
【样例2】
3
2 3
-1 -1
-1 -1
1 2 3
3
2 3
-1 -1
-1 -1
1 3 2
样例输出 Sample Output
【样例1】
true
【样例2】
false
数据范围及提示 Data Size & Hint
【样例1】
image.png

【样例2】
image.png

n<=10
2022/4/15 22:45
加载中...