PAT 1102. Invert a Binary Tree (25)
来源:互联网 发布:centos iscsi 编辑:程序博客网 时间:2024/05/29 19:32
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 Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node from 0 to N-1, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
Sample Input:81 -- -0 -2 7- -- -5 -4 6Sample Output:
3 7 2 6 4 0 5 16 5 7 4 3 2 0 1
提交代码
提示: 输入中未出现的编号为根结点, 从根节点开始建树。注意建树时左孩子为右端字符。AC代码:
#include <bits/stdc++.h>#define PI acos(-1)#define INF 0x3f3f3f3f#define SCD(a) scanf("%d",&a)#define SCDD(a,b) scanf("%d%d",&a,&b)#define SCF(a) scanf("%lf",&a)#define PTD(a) printf("%d", a)#define PTDE(a) printf("%d\n",a)#define MST(a,b) memset(a, b, sizeof(a))typedef long long LL;using namespace std;const int M = 510;int n, m;bool use[M], flag;struct w{ int l; int r;};w a[M];struct BSTNode{ int data; BSTNode* lchild; BSTNode* rchild;};BSTNode* T;void init(){ MST(use, false);}void CreateBSTree(BSTNode* &T, int val){ if(!T){ T = new BSTNode; T->data = val; T->lchild = NULL; T->rchild = NULL; } if( a[val].l != -1 ) CreateBSTree(T->rchild, a[val].l); if( a[val].r != -1 ) CreateBSTree(T->lchild, a[val].r);}void LevelTraverse(){ queue<BSTNode*> q; BSTNode* p; p = T; q.push(p); flag = true; while(!q.empty()){ p = q.front(); q.pop(); if(flag){ flag = false; }else{ cout<<" "; } cout<<p->data; if(p->lchild) q.push(p->lchild); if(p->rchild) q.push(p->rchild); } cout<<endl; flag = true;}void InOrderTraverse(BSTNode* &T){ if(!T) return; if(T->lchild) InOrderTraverse(T->lchild); if(flag) flag = false; else cout<<" "; cout<<T->data; if(T->rchild) InOrderTraverse(T->rchild);}void solve(){ int i; char c; for(i=0; i<n; i++){ cin>>c; if(c == '-'){ a[i].l = -1; }else{ a[i].l = c - 48; use[ a[i].l ] = true; } cin>>c; if(c == '-'){ a[i].r = -1; }else{ a[i].r = c - 48; use[ a[i].r ] = true; } } for(i=0; i<n; i++){ if(!use[i]){ CreateBSTree(T, i); break; } }}int main(){ int i ,j; SCD(n); init(); solve(); LevelTraverse(); InOrderTraverse(T); cout<<endl; return 0;}
- [PAT]1102. Invert a Binary Tree (25)
- 【PAT】1102. Invert a Binary Tree (25)
- PAT 1102. Invert a Binary Tree (25)
- PAT 1102. Invert a Binary Tree (25)
- pat 1102. Invert a Binary Tree (25)
- PAT 1102. Invert a Binary Tree (25)
- 【PAT】1102. Invert a Binary Tree (25)
- PAT 1102. Invert a Binary Tree (25)
- PAT 1102. Invert a Binary Tree (25)
- PAT(A) - 1102. Invert a Binary Tree (25)
- PAT(A)-1102. Invert a Binary Tree (25)(数据结构+bfs)
- PAT A 1102. Invert a Binary Tree (25)
- Pat(A) 1102. Invert a Binary Tree (25)
- PAT--1102. Invert a Binary Tree
- 【PAT】1102. Invert a Binary Tree
- PAT A1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)-PAT甲级真题
- 1102. Invert a Binary Tree (25) PAT甲级
- idea开发工具破解
- Linux SPI总线和设备驱动架构之四:SPI数据传输的队列化
- 类加载机制之ClassLoader
- 算法程序设计 最小公倍数
- 【深度】金融科技能否促进公民权利?
- PAT 1102. Invert a Binary Tree (25)
- 【行业】施乐证明?复印机巨头探寻区块链时间戳战略
- TypeScript入门知识三(表达式和循环)
- Object转byte[]←→byte[]转Object
- 使用maven搭建ssh框架,以及遇到的大小问题
- python-enumerate()
- ThreadStart()笔记
- 小型直播系统系列-乐聊TV的开发(一)
- [cocos2dx_Lua]在发布时加密lua源文件