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

    树的层序遍历和中序遍历; 方法和普通的遍历一样,只不过这里要求翻转一棵树,只需要遍历的时候先遍历右子树,再遍历左子树就行了;
    题目中没给出根节点,在输入中记录每一个出现的数字, 0~N-1中没有出现的数字就是根节点;
     1 #include<iostream>
     2 #include<vector>
     3 #include<queue>
     4 using namespace std;
     5 vector<vector<int>> v(10);
     6 vector<int> inorder, exist(10, 1), level;
     7 
     8 void dfs(int root){
     9   if(root==-1) return;
    10   if(v[root][1]!=-1) dfs(v[root][1]);
    11   inorder.push_back(root);
    12   if(v[root][0]!=-1) dfs(v[root][0]);
    13 }
    14 
    15 int main(){
    16   int n, i, root, left, right;
    17   char l, r;
    18   cin>>n;
    19   for(i=0; i<n; i++){
    20     cin>>l>>r;
    21     left = l=='-' ? -1 : l-'0';
    22     right = r=='-' ? -1 : r-'0';
    23     if(left>=0) exist[left]=0;
    24     if(right>=0) exist[right]=0;
    25     v[i].push_back(left); v[i].push_back(right);
    26   }
    27   for(i=0; i<n; i++) if(exist[i]) root=i;
    28   queue<int> q;
    29   q.push(root);
    30   while(q.size()){
    31     int temp=q.front();
    32     q.pop();
    33     level.push_back(temp);
    34     if(v[temp][1]!=-1) q.push(v[temp][1]);
    35     if(v[temp][0]!=-1) q.push(v[temp][0]);
    36   }
    37   dfs(root);
    38   cout<<level[0];
    39   for(i=1; i<n; i++) cout<<" "<<level[i];
    40   cout<<endl;
    41   cout<<inorder[0];
    42   for(i=1; i<n; i++) cout<<" "<<inorder[i];
    43   cout<<endl;
    44   return 0;
    45 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    cURL(wget)—— 测试 RESTful 接口及模拟 GET/POST/PUT/DELETE/OPTIONS 请求
    docker 第一课 —— 从容器到 docker
    docker 第一课 —— 从容器到 docker
    多类 SVM 的损失函数及其梯度计算
    软RAID 0的技术概要及实现
    用 Linux blkid 命令查找块设备详情
    常用命令
    Ubuntu---samba(安装、配置、使用)***
    系统常用命令
    在64位的UBUBTU 服务器 ***
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9213567.html
Copyright © 2020-2023  润新知