#include <bits/stdc++.h> using namespace std; typedef struct T { char data[4]; struct T *rchild,*lchild; } TNode,*Tree; char a[100][3]; //模拟树的每层的字符 int depth = 0; void ini_T(Tree &T) { T = new TNode; T->lchild = NULL; T->rchild = NULL; } void Create_T(Tree &t) { if(a[depth][0] == '#') { t = NULL; depth++; //当一个结点为空时意味着该结点的父节点创建成功,深度+1 } else { t = new TNode; strcpy(t->data,a[depth++]); Create_T(t->lchild); Create_T(t->rchild); } } void MINTrave(Tree t,int dep) //镜像中序遍历二叉树 { if(t) { MINTrave(t->rchild,dep+1); //先遍历右子树 int n = dep * 4 ; for(int i = 0; i < n; ++i) //每层先输出4倍层数的空格 cout << ' '; cout << t->data << endl; MINTrave(t->lchild,dep+1); } } int main() { char b[1000]; while(gets(b) != NULL) { Tree T; ini_T(T); int i, p = 0,q = 0; for(i = 0 ; b[i] ; ++i) { if(b[i] != ' ') a[p][q++] = b[i]; else //遇到空格时,该结点创建完毕 { a[p++][q] = '\0'; //p++ 移到下一个结点 q = 0; } } a[p++][q++] = '\0'; Create_T( T ); MINTrave( T, 0 ); depth = 0; //将深度更新为0 cout<<endl; } return 0; } 的时间复杂度和空间复杂度
理解问题#include <bits/stdc++.h> using namespace std; typedef struct T { char data[4]; struct T *rchild,*lchild; } TNode,*Tree; char a[100][3]; //模拟树的每层的字符 int depth = 0; void ini_T(Tree &T) { T = new TNode; T->lchild = NULL; T->rchild = NULL; } void Create_T(Tree &t) { if(a[depth][0] == '#') { t = NULL; depth++; //当一个结点为空时意味着该结点的父节点创建成功,深度+1 } else { t = new TNode; strcpy(t->data,a[depth++]); Create_T(t->lchild); Create_T(t->rchild); } } void MINTrave(Tree t,int dep) //镜像中序遍历二叉树 { if(t) { MINTrave(t->rchild,dep+1); //先遍历右子树 int n = dep * 4 ; for(int i = 0; i < n; ++i) //每层先输出4倍层数的空格 cout << ' '; cout << t->data << endl; MINTrave(t->lchild,dep+1); } } int main() { char b[1000]; while(gets(b) != NULL) { Tree T; ini_T(T); int i, p = 0,q = 0; for(i = 0 ; b[i] ; ++i) { if(b[i] != ' ') a[p][q++] = b[i]; else //遇到空格时,该结点创建完毕 { a[p++][q] = '\0'; //p++ 移到下一个结点 q = 0; } } a[p++][q++] = '\0'; Create_T( T ); MINTrave( T, 0 ); depth = 0; //将深度更新为0 cout<<endl; } return 0; } 的时间复杂度和空间复杂度
已完成理解「#include <bits/stdc++.h> using namespace std; typedef struct T { char data[4]; struct T *rchild,*lchild; } TNode,*Tree; char a[100][3]; //模拟树的每层的字符 int depth = 0; void ini_T(Tree &T) { T = new TNode; T->lchild = NULL; T->rchild = NULL; } void Create_T(Tree &t) { if(a[depth][0] == '#') { t = NULL; depth++; //当一个结点为空时意味着该结点的父节点创建成功,深度+1 } else { t = new TNode; strcpy(t->data,a[depth++]); Create_T(t->lchild); Create_T(t->rchild); } } void MINTrave(Tree t,int dep) //镜像中序遍历二叉树 { if(t) { MINTrave(t->rchild,dep+1); //先遍历右子树 int n = dep * 4 ; for(int i = 0; i < n; ++i) //每层先输出4倍层数的空格 cout << ' '; cout << t->data << endl; MINTrave(t->lchild,dep+1); } } int main() { char b[1000]; while(gets(b) != NULL) { Tree T; ini_T(T); int i, p = 0,q = 0; for(i = 0 ; b[i] ; ++i) { if(b[i] != ' ') a[p][q++] = b[i]; else //遇到空格时,该结点创建完毕 { a[p++][q] = '\0'; //p++ 移到下一个结点 q = 0; } } a[p++][q++] = '\0'; Create_T( T ); MINTrave( T, 0 ); depth = 0; //将深度更新为0 cout<<endl; } return 0; } 的时间复杂度和空间复杂度」
展开阅读网页