zoukankan      html  css  js  c++  java
  • [Jobdu] 题目1521:二叉树的镜像

    不知道怎么回事下面的代码通过了4个测试用例,还有1个测试用例始终是Runtime Error,各位帮我看一下是哪里出了问题

    镜像输出两种方法,一种是递归进行调整,另外一种就是直接在先序遍历的基础上进行改造,下面代码中实现的是第二种

     1 #include <cstdio>
     2 #include <cstdlib>
     3 
     4 typedef struct BTNode{
     5     int key;
     6     struct BTNode *lchild;
     7     struct BTNode *rchild;
     8 }BTNode;
     9 
    10 BTNode *createBinaryTree(int a[], int n) {
    11     BTNode *nodes[n];
    12     for (int i = 0; i < n; ++i) {
    13         nodes[i] = (BTNode *) malloc(sizeof(BTNode));
    14         nodes[i]->key = a[i];
    15         nodes[i]->lchild = NULL;
    16         nodes[i]->rchild = NULL;
    17     }
    18 
    19     for (int i = 0; i < n; ++i) {
    20         char str[10];
    21         scanf("%s", str);
    22         if (str[0] == 'd') {
    23             int left, right;
    24             scanf("%d %d", &left, &right);
    25             nodes[i]->lchild = nodes[left - 1];
    26             nodes[i]->rchild = nodes[right - 1];
    27         } else if (str[0] == 'l') {
    28             int left;
    29             scanf("%d", &left);
    30             nodes[i]->lchild = nodes[left - 1];
    31         } else if (str[0] == 'r') {
    32             int right;
    33             scanf("%d", &right);
    34             nodes[i]->rchild = nodes[right - 1];
    35         }
    36     }
    37 
    38     return nodes[0];
    39 }
    40 
    41 /*
    42 void getTreeMirror(BTNode *root) {
    43     if (!root)
    44         return;
    45     if (!root->lchild && !root->rchild)
    46         return;
    47 
    48     BTNode *temp = root->lchild;
    49     root->lchild = root->rchild;
    50     root->rchild = temp;
    51 
    52     getTreeMirror(root->lchild);
    53     getTreeMirror(root->rchild);
    54 }*/
    55 
    56 void printTreeMirror(BTNode *root, int count) {
    57     if (root) {
    58         count == 0 ? printf("%d", root->key) : printf(" %d", root->key);
    59         printTreeMirror(root->lchild, count + 1);
    60         printTreeMirror(root->rchild, count + 1);
    61     }
    62 }
    63 
    64 int main() {
    65     int n;
    66     while (scanf("%d", &n) != EOF) {
    67         int a[n];
    68         for (int i = 0; i < n; i++)
    69             scanf("%d", &a[i]);
    70 
    71         BTNode *root = createBinaryTree(a, n);
    72         printTreeMirror(root, 0);
    73         printf("
    ");
    74     }
    75 
    76     return 0;
    77 }
    78 /**************************************************************
    79     Problem: 1521
    80     User: tonyhu
    81     Language: C++
    82     Result: Runtime Error
    83 ****************************************************************/
  • 相关阅读:
    值得收藏的146条经典民间偏方[转]
    删除暴风文件夹内的stormliv.exe
    【转】VLAN技术浅谈
    [转载]双击.dsw文件时另开VC6.0,而不会关掉原来已打开的项目的解决办法(转载)
    JVM系列1:Java内存区域
    并发系列3:Lock锁以及核心类AQS
    并发系列1:并发基础知识
    JVM系列2:垃圾收集器与内存分配策略
    JVM系列3:类加载机制
    源码解析之AQS源码解析
  • 原文地址:https://www.cnblogs.com/tonyhu1993/p/4702674.html
Copyright © 2011-2022 走看看