• 二叉树的层次遍历


    题意:输入一棵树,你的任务是从上到下,从左到右输出各个结点的值,输入(11,LL)表示结点的值是11,位置是从根节点访问两次左子树。


    解题思路:此题需要先构造一棵二叉树,有两种方法来表示二叉树,一是指针表示,而是数组表示。   
    1.此题在访问的时候用到了队列                                                                                 
    2.此题在输入的时候也有一些小技巧,例如,sscanf()函数输入某个字符串,还有strchr()函数get某些字符,                                                 
    3.failed标记是否被访问过 
    4.get一种新的输出方式 printf("%d%c", ans[i], i == ans.size()-1 ? ' ' : ' ');

    指针表示:

     1 #include<stdio.h>
     2 #include<string>
     3 #include<cstring>
     4 #include<queue>
     5 #include<vector>
     6 #include<iostream>
     7 using namespace std;
     8 const int MAXX=300;
     9 char s[MAXX];
    10 bool failed;
    11 
    12 struct node{
    13     int value;
    14     bool have_value;
    15     node *left,*right;
    16     node():have_value(false),left(NULL),right(NULL){}
    17 };
    18 node* root;
    19 
    20 void addnode(int v,char* s){                    //将结点加入树
    21     int n=strlen(s);
    22     node* u=root;
    23     for(int i=0;i<n;i++){
    24         if(s[i]=='L'){
    25             if(u->left==NULL) u->left=new node();
    26             u=u->left;
    27         }
    28         else if(s[i]=='R'){
    29             if(u->right==NULL) u->right=new node();
    30             u=u->right;
    31         }
    32     }
    33     if(u->have_value) failed=true;
    34     u->value=v;
    35     u->have_value=true;
    36 }
    37 
    38 void remove_tree(node *u){              //释放内存,防止泄露
    39     if(u==NULL) return;
    40     remove_tree(u->left);
    41     remove_tree(u->right);
    42     delete u;
    43 }
    44 
    45 bool read_input(){
    46     failed=false;
    47     remove_tree(root);                            //结点的输入
    48     root=new node();
    49     for(;;){
    50         if(scanf("%s",s)!=1) return false;
    51         if(!strcmp(s,"()")) break;
    52         int v;
    53         sscanf(&s[1],"%d",&v);
    54         addnode(v,strchr(s,',')+1);
    55     }
    56     return true;
    57 }
    58 
    59 bool bfs(vector<int>& ans){    //广度优先搜索,从左至右依次找结点
    60     queue<node*> q;
    61     ans.clear();
    62     q.push(root);
    63     while(!q.empty()){
    64         node *u=q.front();
    65         q.pop();
    66         if(!u->have_value) return false;
    67         ans.push_back(u->value);
    68         if(u->left!=NULL) q.push(u->left);
    69         if(u->right!=NULL) q.push(u->right);
    70     }
    71     return true;
    72 }
    73 
    74 int main(){
    75     vector<int> ans;
    76     freopen("in.txt","r",stdin);
    77     while(read_input()){
    78       //  cout<<failed;
    79         if(!failed&&bfs(ans)){
    80             for(int i=0;i<ans.size();i++){
    81                 printf("%d%c", ans[i], i == ans.size()-1 ? '
    ' : ' ');
    82 
    83             }
    84         }
    85         else{
    86             printf("not complete
    ");
    87         }
    88     }
    89 }
  • 相关阅读:
    Step By Step(C++模板类)
    Step By Step(C++模板重载和特化)
    Step By Step(C++模板推演)
    Step By Step(C++模板Policy)
    Step By Step(C++模板参数)
    Step By Step(C++模板解析)
    Step By Step(C++模板Trait)
    Step By Step(C++模板基本技巧)
    离职引发的诸多感触
    Step By Step(C++模板函数)
  • 原文地址:https://www.cnblogs.com/muziqiu/p/7239984.html
Copyright © 2020-2023  润新知