• 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("
    ");
    95     inoder(root);
    96     printf("
    ");
    97     return 0;
    98 }
  • 相关阅读:
    Objective-C Runtime 运行时之四:Method Swizzling
    App启动加载广告页面思路
    关于CoreData和SQLite多线程访问时的线程安全问题
    HIVE学习(待更新)
    流处理环境搭建
    CAJ2PDF
    ArcMap加载在线地图
    学习opencv(持续更新)
    风险和策略(待更新)
    区块链入门教程(转)
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5218874.html
Copyright © 2020-2023  润新知