MOOC Zhejiang University Data Structure-03-Tree 2 List Leaves (25 puntos)

El código se basa en la
"Estructura de datos" 03-Tree 2 List Deja la
idea de resolución de problemas escrita en este artículo : atraviesa el árbol en una secuencia de
niveles 1. El uso de la variable de bandera no puede ser escrito por mí mismo.

        if(t.Left==Null&&t.Right==Null){
    
    
            if(flag){
    
    
                cout<<" ";
            }
            else{
    
    
                flag = true;        
            }
            cout<<t.Data;
        }

2. Al principio, los datos de la estructura TreeNode no se escribieron, lo que provocó que no se supiera qué datos generar.
3.using namespace std; // Si escribe, informará de un
error ' cout ' no fue declarado en este alcance 4.pop no tiene valor de retorno, pensé erróneamente que el elemento devuelto era el popup.

#include<iostream>
#include<queue>
#define Null -1
#define MAXSIZE 10 //不需要加分号
using namespace std;//如果写会报错‘cout’ was not declared in this scope
typedef int Tree;
struct TreeNode{
    
    
    int Data;  //使用这个Data来存放结构数组T内每个节点存储的位置,便于输出结果
    Tree Left;
    Tree Right;
}T[MAXSIZE];
Tree BuildTree(struct TreeNode T[]);
void Traversal(int root);
Tree BuildTree(struct TreeNode T[]){
    
    
    int Root=Null,N; //刚开始将节点置为空,若为空树的时候可返回Null
    char cl,cr;
    scanf("%d",&N);
    if(N){
    
       //加入对树为空的判断
    int check[MAXSIZE]={
    
    0};//置为零
        for(int i=0;i<N;++i){
    
       //把N个节点读入树
            //C语言输入:scanf("\n%c %c",&cl,&cr);//&T[i].Data,&cl,&cr是不是不需要&  蒙
            //C++输入见下一行,似乎更简单了
            cin>>cl>>cr;
            T[i].Data = i;
            if(cl!='-'){
    
      //将单引号错写为双引号会报错comparison between pointer and integer
                T[i].Left = cl - '0';
                check[T[i].Left] = 1;
            }
            else{
    
    
                T[i].Left = Null;
            }
            if(cr!='-'){
    
    
                T[i].Right = cr - '0';
                check[T[i].Right] = 1;
            }
            else{
    
    
                T[i].Right = Null;
            }

        }
        int i;
        for(i=0;i<N;++i){
    
    
            if(!check[i]) break;        
        }
        Root = i; 
    }
    return Root;    
}
void Traversal(int root){
    
    
    if(root==Null) {
    
    
        cout<<"-";
        return;  //结束函数下运行
    }
    bool flag = false;
    struct TreeNode t;
    queue<struct TreeNode> q;
    q.push(T[root]);
    while(!q.empty()){
    
    
        t = q.front();
        q.pop();  //队列中的pop没有返回值么
        if(t.Left==Null&&t.Right==Null){
    
    
            if(flag){
    
    
                cout<<" ";
            }
            else{
    
    
                flag = true;        
            }
            cout<<t.Data;
        }
        //以下两个if 我没十分搞清楚应该放在循环里还是外面,如果放在里面第一次循环pop出根节点之后队列为空,不再循环了
        if(t.Left!=Null){
    
       
            q.push(T[t.Left]);
        }
        if(t.Right!=Null){
    
    
            q.push(T[t.Right]);
        }
    }

}

int main(){
    
    
    int r;
    r = BuildTree(T);
    Traversal(r);
    return 0;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_43919570/article/details/105124032
Recomendado
Clasificación