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 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 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

这题是依次给出节点的左孩子右孩子,进行找出翻转二叉树的层序遍历和中序遍历。

我们可以使用中序遍历进行构建树,之后进行使用sort根据level进行从小到大,根据index进行从大到小。即可获得翻转二叉树的层序遍历。

最后打印

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct node {
    int index, id, left = -1, right = -1, level;
}a[100];
int N;
vector<node> v;
bool cmp(node& n1, node& n2){
    return n1.level == n2.level ? n1.index > n2.index: n1.level < n2.level;
}
void dfs(int root, int index, int level){
    if(a[root].right != -1) dfs(a[root].right, index * 2 + 2, level + 1);
    v.push_back({index, root, 0, 0, level});
    if(a[root].left != -1) dfs(a[root].left, index * 2 + 1, level + 1);
}
int main(){
    cin >> N;
    string tmp_l, tmp_r;
    vector<bool> find_root(N);
    for(int i = 0; i < N; i++){
        cin >> tmp_l >> tmp_r;
        a[i].id = i;
        if(tmp_l != "-") {
            a[i].left = stoi(tmp_l);
            find_root[stoi(tmp_l)] = true;
        }
        if(tmp_r != "-") {
            a[i].right = stoi(tmp_r);
            find_root[stoi(tmp_r)] = true;
        }
    }
    int root_index;
    for(int i = 0; i < N; i++)
        if(!find_root[i]) root_index = i;
    dfs(root_index, 0, 0);
    vector<node> v2(v);
    sort(v2.begin(), v2.end(), cmp);
    for(int i = 0; i < N; i++)
        if(i != N-1) cout << v2[i].id << " ";
        else cout << v2[i].id <<endl;
    for(int i = 0; i < N; i++)
        if(i != N-1) cout << v[i].id << " ";
        else cout << v[i].id <<endl;
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/littlepage/p/12234525.html

时间: 2024-09-30 22:11:18

PAT Advanced 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.

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 (Advanced Level) 1110. Complete Binary Tree (25)

判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n,root; const int maxn=30; struc

【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

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 1055 The World&#39;s Richest (25分)

Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That is, given the n

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

1066 Root of AVL Tree (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

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