[] NOIP symmetrical binary tree

[] NOIP symmetrical binary tree

topic

Title Description

The right to have a little bit of the root of the tree if the following conditions are met Xuan Xuan were called symmetric binary tree:

Binary tree;
the same left and right subtrees of all these trees switching nodes, the new tree and the original tree structure corresponding to the location points and equal rights.
The numbers in the figure is the node weights, idid outer nodes represent the node number.

Here Insert Picture Description

Now we are given a binary tree, I hope you find it a sub-tree, the tree is symmetrical sub binary tree, and a maximum number of nodes. Please tree output sub-tree nodes.

Note: Only the roots of the trees are symmetrical binary tree. In this problem convention, to a sub-root node of T "subtree" means: binary tree node T and all of its successor nodes.

Input Format

The first line of a positive integer n, the number of a given node of a tree, a predetermined node number 1~n, where 1 is the root node.

The second row of n positive integers, separated by a space, the i-th positive integer weights vi for node i. Next n lines of two positive integers L_i, r_i , each node i represents the number of children left. If the left / right child does not exist, -1 means. Separated by a space between the two numbers.

Output Format

Total output file row contains an integer indicating the maximum tree given two symmetrical fork tree nodes.

Sample input and output

Copy Input # 1
2
1. 3
2 -1
-1 -1
output # 1 Copy
1

Copy Input # 2
10
2. 5. 5. 4. 5. 5 2 2. 4. 3
. 9 10
-1 -1
-1 -1
-1 -1
-1 -1
-1 2
. 3. 4
. 5. 6
-1 -1
. 7. 8
output copy # 2
3

Here Insert Picture Description
Here Insert Picture Description

analysis

Those who do not understand the beginning of sample data, to see more than a little muddled. . . . . Later still need to explain the large cattle, able to read.
Need to see more than a few questions, the samples are at their first simulation, it is good to understand.

1 first reads data,
int v[maxn],l[maxn],r[maxn]; //v权值,l左孩子编号 ,r右孩子编号
2 and then we start traversing from the first node to see if the node is symmetrical about the child, if the child is about to look at whether the child about symmetry,
until the child is not around, or asymmetry. Asymmetric, then put the second node as the root, repeated comparison. . . . . . . .
3 Each time a relatively large value, the final output

Local codes may have doubts:

f(l[x],r[y]);					//如果该点的左右孩子对称,								
	f(r[x],l[y]);					//看该点的左右孩子的子树是否对称 

As shown, symmetrical about children with node id 1, then see if it's about the child's symmetrical about children,
pay attention to this point is the left child of the left child and the right child of the right child than the right, the left and right of the child and the right child children's children than to the left
to see the code when he got it.
Here Insert Picture Description

Other code is very detailed. .

Code

#include<iostream>
#include<cstdio>
using namespace std;

const int maxn = 1e7;

int n;
int v[maxn],l[maxn],r[maxn];	//v权值,l左孩子编号 ,r右孩子编号 

int ans ;	//答案 
int is ; 	//是不是对称 

void f(int x,int y){		//检查是否对称 
	if(x == -1 && y == -1){				//如果没有左右孩子,return 
		return ;
	}
	if(x == -1 || y == -1 || v[x] != v[y]){	//如果不是对称 
		is = 0;								//0表示不是对称 
		return ;
	}
	f(l[x],r[y]);					//如果该点的左右孩子对称,								
	f(r[x],l[y]);					//看该点的左右孩子的子树是否对称 
}

int cnt(int x){				//累加节点数 
	int k=1;
	if(l[x] != -1 ){
		k += cnt(l[x]); 
	}
	if(r[x] != -1 ){
		k += cnt(r[x]);
	}	
	return k;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=1;i<=n;i++){
		scanf("%d%d",&l[i],&r[i]);
	}
	
	//从最前的节点遍历 
	for(int i=1;i<=n;i++){
		 is = 1;		//开始先默认是对称 
		f(l[i],r[i]);	//检查是否对称 
		if(is){			//如果是对称,那就比较取较大值。 
			ans = max(ans,cnt(i));
		}
	}
	cout<<ans<<endl;
	return 0;
}
Published 75 original articles · won praise 1 · views 3634

Guess you like

Origin blog.csdn.net/A793488316/article/details/104744141