SWUST OJ 1053: Output C++ implementation of degree of specified node in binary tree created by preorder traversal

topic description

Create a binary tree using the preorder recursive traversal algorithm and output the degree of the specified node in the binary tree. It is agreed that the node data of the binary tree is a single uppercase English character. When the received data is the character "#", it means that the node does not need to be created, otherwise the node is created. Finally, output the degree of the specified node in the created binary tree. Pay attention to the sequence and number relationship between the characters "#" and non-"#" characters in the input data sequence, which will ultimately determine the shape of the created binary tree.

enter

The input case is divided into 2 lines. The first line accepts a string composed of uppercase English characters and "#" characters entered by the keyboard (used to create the corresponding binary tree), and the second line specifies the node data.

output

#include<bits/stdc++.h>
using namespace std;
char cmp;
int ans = 0;
struct{
	int l, r;
}z[105];
int creat(){
	char ch;
	cin>>ch;
	if(ch=='#') return ch;
	z[ch].l = creat();
	z[ch].r = creat();
	return ch;
}
int main(){
	creat();
	cin>>cmp;
	if(z[cmp].l!='#') ans++;
	if(z[cmp].r!='#') ans++;
	cout<<ans;
	return 0;
}

Output the degree of the specified node in the binary tree corresponding to this use case in one line.

 

Guess you like

Origin blog.csdn.net/Ljy_Cxy/article/details/131465172