7.29

1043 Is It a Binary Search Tree (25)(25 分)提问

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.

Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not. Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:

7
8 6 5 7 10 8 11

Sample Output 1:

YES
5 7 6 8 11 10 8

Sample Input 2:

扫描二维码关注公众号,回复: 2543653 查看本文章
7
8 10 11 8 6 7 5

Sample Output 2:

YES
11 8 10 7 5 6 8

Sample Input 3:

7
8 6 8 5 10 9 11

Sample Output 3:

NO
#include <cstdio>
#include <vector>
using namespace std;
struct node{
	int data;
	node * left, *right;
};

void insert(node *&root,int data)
{
	if(root==NULL)//到达空结点 就是需要插入的位置
   {
   	  root=new node;
   	root->data=data; 
   	root->left=root->right=NULL;
   	return;
   } 
   if(data<root->data) insert(root->left,data);
   else insert(root->right,data);
}

void preorder(node *root,vector<int >&vi)//先序遍历结果存放在vi 
{
	if(root==NULL) return ;
	vi.push_back(root->data);
	preorder(root->left,vi);
	preorder(root->right,vi);
}

//镜像先序遍历 结果存放在vi 
void pminor(node *root,vector<int > &vi)
{
	if(root==NULL)  return ;
	vi.push_back(root->data);
	pminor(root->right,vi);
	pminor(root->left,vi);
}

void postorder(node *root,vector<int> &vi)
{
	if(root==NULL) return ;
	
	postorder(root->left,vi);
	postorder(root->right,vi);
	vi.push_back(root->data);
}

//镜像树后序遍历 结果存放于vi
void  mpostorder(node *root,vector<int> &vi)
{
	if(root==NULL) return ;
	mpostorder(root->right,vi);
	mpostorder(root->left,vi);
	vi.push_back(root->data);
}

//origin存放初始序列 
vector<int> origin,pre,prem,post,postm;
int main()
{
	int n,data;
	node *root=NULL;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&data);
		origin.push_back(data);
		insert(root,data);
	}
	preorder(root,pre);
	pminor(root,prem);
	postorder(root,post);
	mpostorder(root,postm);
	if(origin==pre)
	{
		printf("YES\n");
		for(int i=0;i<post.size();i++)
		{
			if(i>0)
			printf(" ");
			printf("%d",post[i]); 
		}

	}
	else if(origin==prem)
	{
		printf("YES\n");
	for(int i=0;i<postm.size();i++)
		{
			if(i>0)
			printf(" ");
			printf("%d",postm[i]); 
		}
	}
	else printf("NO\n");
	return 0;
}





1064 Complete Binary Search Tree (30)(30 分)提问

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input:

10
1 2 3 4 5 6 7 8 9 0

Sample Output:

6 3 8 1 5 7 9 0 2 4
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1010;

int n,number[maxn],CBT[maxn],index=0;

void inorder(int root)//中序遍历
{
	if(root>n) return;
	inorder(root*2);
	CBT[root]=number[index++];
	inorder(root*2+1);
	
} 


int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	  scanf("%d",&number[i]);
	sort(number,number+n);
	inorder(1);
	for(int i=1;i<=n;i++)
	{
		printf("%d",CBT[i]);//本身即是层序 
		if(i<n) printf(" ");
	}

	return 0;
}

1099 Build A Binary Search Tree (30)(30 分)提问

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

The left subtree of a node contains only nodes with keys less than the node's key.

The right subtree of a node contains only nodes with keys greater than or equal to the node's key.

Both the left and right subtrees must also be binary search trees.

Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format "left_index right_index", provided that the nodes are numbered from 0 to N-1, and 0 is always the root. If one child is missing, then -1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.

Output Specification:

For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.

Sample Input:

9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42

Sample Output:

58 25 82 11 38 67 45 73 42
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

const int maxn=110;
struct node{
	int data;
	int lchild,rchild;
}Node[maxn];//二叉树的静态写法 
 int n,in[maxn], num=0;
 //中序遍历 将排序好的序列一次填入二叉树结点
 void inorder(int root)
 {
 	if(root==-1)  return;
 	inorder(Node[root].lchild);
 	Node[root].data=in[num++];//填入序列中的整数 
 	inorder(Node[root].rchild);
 } 
 
 void BFS(int root)
 {
 	queue <int> q;
 	q.push(root);
 	num=0;
 	while(!q.empty())
 	{
 		int now=q.front();
 		q.pop();
 		printf("%d",Node[now].data);
 		num++;
 		if(num<n) printf(" ");
 		if(Node[now].lchild!=-1) q.push(Node[now].lchild);
 		if(Node[now].rchild!=-1) q.push(Node[now].rchild);
 	}
 	
 	
 }
 
 int main()
 {
 	int lchild,rchild;
 	scanf("%d",&n);
 	for(int i=0;i<n;i++)
 	{
 		scanf("%d%d",&lchild,&rchild);
 		Node[i].lchild=lchild;
 		Node[i].rchild=rchild;
 	}
 	for(int i=0;i<n;i++)
 	scanf("%d",&in[i]);
 	sort(in,in+n);
 	inorder(0);
 	BFS(0);
 	return 0; 
 }
 
 
 
 

1066 Root of AVL Tree (25)(25 分)提问

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

 

 

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the root of the resulting AVL tree in one line.

Sample Input 1:

5
88 70 61 96 120

Sample Output 1:

70

Sample Input 2:

7
88 70 61 96 120 90 65

Sample Output 2:

88
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
	int v,height;//v为结点权值 height为当前子树高度
	node *lchild,*rchild; 
}*root;
node *newnode(int v)
{
	node *Node=new node;
	Node->v=v;
	Node->height=1;
	Node->lchild=Node->rchild=NULL;
	return Node;
} 

//获取以root为根节点的子树的当前height
int getheight(node *root)
{
	if(root==NULL) return 0;//空结点高度为0 
	return root->height;
} 

//更新结点的height
void update(node *root)
{
	root->height=max(getheight(root->lchild),getheight(root->rchild))+1;
} 

//计算平衡因子
int getbalance(node * root)
{
	return getheight(root->lchild)-getheight(root->rchild);
	
} 

//左旋 Left Rotation
void L (node* &root)
{
	node *temp=root->rchild;
	root->rchild=temp->lchild;
	temp->lchild=root;
	update(root);
	update(temp);
	root=temp; 
} 

//右旋 
void R(node* &root)
{
	node *temp=root->lchild;
	root->lchild=temp->rchild;
	temp->rchild=root;
	update(root);
	update(temp);
	root=temp; 
	
} 

//插入权值为v的结点
void insert(node* &root,int v)
{
	if(root==NULL) //到达结点 
	{
		root=newnode(v);
		return ;
	}
    if(v<root->v)
    {
    	insert(root->lchild,v);
    	update(root);
    	if(getbalance(root)==2)
    	{
    		if(getbalance(root->lchild)==1)//LL型 
    		  R(root);
    		else if (getbalance(root->lchild)==-1) //LR型 
    		{
    			L(root->lchild);
    			R(root);
    		}
    	}
      
    }
    else
    {
    	insert(root->rchild,v);
    	update(root);
    	if(getbalance(root)==-2)
    	{
    		if(getbalance(root->rchild)==-1)//RR型 
    		  	L(root);
    		else if (getbalance(root->rchild)==1) //RL型 
    		{
    			R(root->rchild);
    			L(root);
    		}
    		
    }
	
}
} 
//AVL树的建立 
/*node *create(int data[],int n)
{
	node* root=NULL;
	for(int i=0;i<n;i++)
	  insert(root,data[i]);
	
	return root;
} */
int main()
{
	int n,v;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&v);
		insert(root,v);
	}
	printf("%d\n",root->v);
	return 0;
}

1107 Social Clusters (30)(30 分)提问

When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A "social cluster" is a set of people who have some of their hobbies in common. You are supposed to find all the clusters.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (<=1000), the total number of people in a social network. Hence the people are numbered from 1 to N. Then N lines follow, each gives the hobby list of a person in the format:

K~i~: h~i~[1] h~i~[2] ... h~i~[K~i~]

where K~i~ (>0) is the number of hobbies, and h~i~[j] is the index of the j-th hobby, which is an integer in [1, 1000].

Output Specification:

For each case, print in one line the total number of clusters in the network. Then in the second line, print the numbers of people in the clusters in non-increasing order. The numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

8
3: 2 7 10
1: 4
2: 5 3
1: 4
1: 3
1: 4
4: 6 8 1 5
1: 4

Sample Output:

3
4 3 1
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=1010;
int father[N];//存放父亲结点
int isroot[N]={0};//记录每个结点是否作为集合的根节点
int course[N]={0};

int findfather(int x)//寻找x所在集合的根节点 
{
	int a=x;
	while(x!=father[x])
	x=father[x];
	//路径压缩
	while(a!=father[a])
	{
		int z=a;
		a=father[a];
		father[z]=x;//;路径上的所有都放到x下 
	} 
	return x;
} 

void Union(int a,int b)//合并ab集合
{
	int faA=findfather(a);
	int faB=findfather(b);
	if(faA!=faB)
	  father[faA]=faB;
} 

void init(int n)
{
	for(int i=1;i<=n;i++)
	{
		father[i]=i;
		isroot[i]=false;
	}
	
}
bool cmp(int a,int b)
{
	return a>b;
}

int main()
{
	int n,k,h;
	scanf("%d",&n);
	init(n);
	for(int i=1;i<=n;i++)//对每个人 
	{
		scanf("%d:",&k);
		for(int j=0;j<k;j++)
		{
			scanf("%d",&h);
			if(course[h]==0)//第一次有人喜欢
			course[h]=i; 
			Union(i,findfather(course[h]));
		}
	
	}
	for(int i=1;i<=n;i++)
	{
		isroot[findfather(i)]++;
	}
	int ans=0;//记录集合数目
	for(int i=1;i<=n;i++)
	{
		if(isroot[i]!=0)
		{
			ans++;
		}
	} 
	printf("%d\n",ans);
	sort(isroot+1,isroot+n+1,cmp);
	for(int i=1;i<=ans;i++)
	{
		printf("%d",isroot[i]);
		if(i<ans) printf(" ");
	}
	
	
	
	
	
	return 0;
}









猜你喜欢

转载自blog.csdn.net/Coding18/article/details/81276197