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:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1
 1 #include<stdio.h>
 2 #include<string>
 3 #include<iostream>
 4 #include<string.h>
 5 #include<sstream>
 6 #include<vector>
 7 #include<map>
 8 #include<stdlib.h>
 9 #include<queue>
10 using namespace std;
11
12 struct node
13 {
14     node():l(-1),r(-1){}
15     int l,r,id;
16 };
17
18 node Tree[15];
19 bool notroot[15];
20 bool fir = 1;
21 void inoder(int root)
22 {
23     if(Tree[root].l != -1)
24     {
25         inoder(Tree[root].l);
26     }
27     if(fir)
28     {
29         fir = 0;
30         printf("%d",root);
31     }
32     else printf(" %d",root);
33     if(Tree[root].r != -1)
34     {
35         inoder(Tree[root].r);
36     }
37 }
38 int main()
39 {
40     int n,tem;
41     char l[5],r[5];
42     scanf("%d",&n);
43     for(int i = 0;i <n;++i)
44     {
45         Tree[i].id = i;
46     }
47     for(int i = 0;i <n;++i)
48     {
49         scanf("%s%s",r,l);
50         if(l[0] != ‘-‘)
51         {
52             tem = atoi(l);
53             Tree[i].l = tem;
54             notroot[tem] = 1;
55         }
56         if(r[0] != ‘-‘)
57         {
58             tem = atoi(r);
59             Tree[i].r = atoi(r);
60             notroot[tem] = 1;
61         }
62     }
63     int root;
64     for(int i = 0;i <n;++i)
65     {
66         if(!notroot[i])
67         {
68             root = i;
69             break;
70         }
71     }
72     queue<node> qq;
73     qq.push(Tree[root]);
74     bool fir2 = 1;
75     while(!qq.empty())
76     {
77         node ntem = qq.front();
78         qq.pop();
79         if(fir2)
80         {
81             fir2 = 0;
82             printf("%d",ntem.id);
83         }
84         else printf(" %d",ntem.id);
85         if(ntem.l != -1)
86         {
87             qq.push(Tree[ntem.l]);
88         }
89         if(ntem.r != -1)
90         {
91             qq.push(Tree[ntem.r]);
92         }
93     }
94     printf("\n");
95     inoder(root);
96     printf("\n");
97     return 0;
98 }
时间: 2024-12-19 09:32:50

1102. Invert a Binary Tree (25)的相关文章

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.

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

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

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

二叉树遍历 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

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 &

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 each case, the first line gives a positive integer N (≤) which is the total number of nodes in the tree -- and he

PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则即不是. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; const int maxn=22; int two

【PAT甲级】1110 Complete Binary Tree (25分)

题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点. trick: 用char输入子结点没有考虑两位数的结点??... stoi(x)可以将x转化为十进制整数 AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace