• 二叉树的遍历


     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <cstdlib>
     6 // #define test cout<<"***"<<endl
     7 // #define test1 cout<<"&&&&"<<endl
     8 
     9 using namespace std;
    10 
    11 typedef struct Tree{
    12     int value;
    13     struct Tree *left;
    14     struct Tree *right;
    15 }Tree ,*Binary_Tree;
    16 
    17 //创建二叉树
    18 void CreatTree(Binary_Tree &T){
    19     int n;
    20     T=(Binary_Tree)malloc(sizeof(Tree));
    21     cin>>n;
    22     if(n==0)
    23     T=NULL;
    24     else{
    25         T->value=n;
    26         CreatTree(T->left);
    27         CreatTree(T->right);
    28     }
    29 }
    30 
    31 //先序遍历
    32 void preshowTree(Binary_Tree T){
    33     if(T!=NULL){
    34         cout<<T->value<<" ";
    35         preshowTree(T->left);
    36         preshowTree(T->right);
    37     }
    38 }
    39 
    40 //中序遍历
    41 void midshowTree(Binary_Tree T){
    42     if(T!=NULL){
    43         midshowTree(T->left);
    44         cout<<T->value<<" ";
    45         midshowTree(T->right);
    46     }
    47 }
    48 
    49 //后序遍历
    50 void finshowTree(Binary_Tree T){
    51     if(T!=NULL){
    52         finshowTree(T->left);
    53         finshowTree(T->right);
    54         cout<<T->value<<" ";
    55     }
    56 }
    57 int main(){
    58     Tree *T;
    59     cout<<"创建一棵二叉树"<<endl;
    60     CreatTree(T);
    61     // test1;
    62     cout<<"先序遍历:"<<" ";
    63     preshowTree(T);
    64     cout<<endl;
    65     cout<<"中序遍历:"<<" ";
    66     midshowTree(T);
    67     cout<<endl;
    68     cout<<"后序遍历:"<<" ";
    69     finshowTree(T);
    70     cout<<endl;
    71     return 0;
    二叉树的三种遍历
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <cstdlib>
     6 // #define test cout<<"***"<<endl
     7 // #define test1 cout<<"&&&&"<<endl
     8 
     9 using namespace std;
    10 
    11 typedef struct Tree{
    12     int value;
    13     struct Tree *left;
    14     struct Tree *right;
    15 }Tree ,*Binary_Tree;
    16 
    17 //创建二叉树
    18 void CreatTree(Binary_Tree &T){
    19     int n;
    20     T=(Binary_Tree)malloc(sizeof(Tree));
    21     cin>>n;
    22     if(n==0)
    23     T=NULL;
    24     else{
    25         T->value=n;
    26         CreatTree(T->left);
    27         CreatTree(T->right);
    28     }
    29 }
    30 
    31 //先序遍历
    32 void preshowTree(Binary_Tree T){
    33     if(T!=NULL){
    34         cout<<T->value<<" ";
    35         preshowTree(T->left);
    36         preshowTree(T->right);
    37     }
    38 }
    39 
    40 //中序遍历
    41 void midshowTree(Binary_Tree T){
    42     if(T!=NULL){
    43         midshowTree(T->left);
    44         cout<<T->value<<" ";
    45         midshowTree(T->right);
    46     }
    47 }
    48 
    49 //后序遍历
    50 void finshowTree(Binary_Tree T){
    51     if(T!=NULL){
    52         finshowTree(T->left);
    53         finshowTree(T->right);
    54         cout<<T->value<<" ";
    55     }
    56 }
    57 int main(){
    58     Tree *T;
    59     cout<<"创建一棵二叉树"<<endl;
    60     CreatTree(T);
    61     // test1;
    62     cout<<"先序遍历:"<<" ";
    63     preshowTree(T);
    64     cout<<endl;
    65     cout<<"中序遍历:"<<" ";
    66     midshowTree(T);
    67     cout<<endl;
    68     cout<<"后序遍历:"<<" ";
    69     finshowTree(T);
    70     cout<<endl;
    71     return 0;
    72 }
  • 相关阅读:
    vj p1034题解
    2010.11.9南高模拟赛
    vj p1041神风堂人数 题解
    noi99钉子和小球 解题报告
    vj p1032题解
    vj p1037题解
    vj p1040题解
    vj p1038题解
    vj p1042捕风捉影 题解
    vj p1046 观光旅游 题解
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/7986080.html
Copyright © 2020-2023  润新知