PAT-1102(Invert a Binary Tree)

  题目见这里

   和1099略微相似,考察二叉树和基本的遍历,算是简单的啦,下标还充当了数据域,主要是知道要标记访问到的下标,从而确定root

//1102:Invert a Binary Tree
#include <cstdio>
#include <iostream> 

using namespace std;

const int N = 10;
typedef struct node{
	int lChild,rChild;
}Node;

void Input(int &root, bool *flag, Node *node, int &n){
	int i;
	char left,right;
	scanf("%d",&n);
	getchar();
	for(i=0;i<n;i++){
		scanf("%c %c",&left,&right);
		getchar();
		if(left==‘-‘) node[i].lChild = -1;
		else{
			node[i].lChild = left-‘0‘;
			flag[left-‘0‘] = true;
		}
		if(right==‘-‘) node[i].rChild = -1;
		else{
			node[i].rChild = right-‘0‘;
			flag[right-‘0‘] = true;
		}
	}
	for(i=0;i<n;i++)
		if(!flag[i]){
			root = i;
			break;
		}
}

void Invert(int root, Node *node){
	if(root!=-1){
		Invert(node[root].lChild,node);
		Invert(node[root].rChild,node);
		swap(node[root].lChild,node[root].rChild);
	}
}

void LevelOrderTraverse(int root, Node *node, int n){
	int qNode[N],tmpNode;
	int front,rear;
	front = rear = 0;
	if(root==-1) return;
	qNode[rear] = root;
	rear ++;
	while(front<rear){ //无‘=‘
		tmpNode = qNode[front];
		front ++;
		printf("%d",tmpNode);
		if(front<n) printf(" ");
		else printf("\n");
		if(node[tmpNode].lChild!=-1){
			qNode[rear]	= node[tmpNode].lChild;
			rear ++;
		}
		if(node[tmpNode].rChild!=-1){
			qNode[rear]	= node[tmpNode].rChild;
			rear ++;
		}
	}
}

void InOrderTraverse(int root, Node *node, int n){
	static int i = 1;
	if(root!=-1){
		InOrderTraverse(node[root].lChild,node,n);
		printf("%d",root);
		if(i==n) printf("\n");
		else printf(" ");
		i ++;
		InOrderTraverse(node[root].rChild,node,n);
	}
}

int main(){
//	freopen("Data.txt","r",stdin);
	Node node[N];
	bool flag[N]={false};
	int root,n;
	Input(root,flag,node,n);
	Invert(root,node);
	LevelOrderTraverse(root,node,n);
	InOrderTraverse(root,node,n);
	return 0;
}
时间: 2024-08-30 08:07:31

PAT-1102(Invert a Binary Tree)的相关文章

1102. Invert a Binary Tree (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1102. Invert a Binary Tree (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.

1102 Invert a Binary Tree (25 分)dfs+层序+中序+后序遍历

1102 Invert a Binary Tree (25 分) The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off. Now it's your turn to prove that YOU CAN i

PAT Advanced 1102 Invert a Binary Tree (25分)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off. Now it's your turn to prove that YOU CAN invert a binary tree! Input Specif

1102. Invert a Binary Tree (25)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off. Now it's your turn to prove that YOU CAN invert a binary tree! Input Specif

PAT (Advanced Level) 1102. Invert a Binary Tree (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct Node { int left; int right; }s[20]; int

PAT甲级——1110 Complete Binary Tree (完全二叉树)

此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For eac

Invert a binary tree 翻转一棵二叉树

假设有如下一棵二叉树: 4  / \   2    7  / \   / \ 1  3 6  9翻转后: 4     /    \    7     2   / \    / \  9  6  3  1 这里采用递归的方法来处理.遍历结点,将每个结点的两个子结点交换位置即可. 从左子树开始,层层深入,由底向上处理结点的左右子结点:然后再处理右子树 全部代码如下: public class InvertBinaryTree { public static void main(String args

二叉树遍历 A1102. Invert a Binary Tree (25) 反转二叉树 并输出层序和中序遍历 (使用静态二叉树)

#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; const int maxn = 110; struct node{ int lchild,rchild; }Node[maxn]; bool notRoot[maxn] = {false};//记录是否不是根节点,初始均是根节点 int n,num = 0

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh