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 ****************************************************************/
  • 相关阅读:
    Git 9. 远程仓库
    Git 8. 删除文件
    Git 7. 撤销修改
    # 并发编程 -进程理论-进程的方法
    socket 上传 -- 异常处理--UDP协议 --自定义socket #29
    socket(套接字)
    面向过程补充 网络编程 #27
    ATM
    选课系统 -- # 25 -26
    面向对象高级 1.反射 2.元类 # 24
  • 原文地址:https://www.cnblogs.com/tonyhu1993/p/4702674.html
Copyright © 2011-2022 走看看