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

The code is based on the
"Data Structure" written in this article 03-Tree 2 List Leaves
Problem Solving Idea: It is enough to traverse the tree in layer order.
1. The use of the flag variable cannot be written by myself.

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

2. At the beginning, the Data in the structure TreeNode was not written, resulting in not knowing what data to output.
3.using namespace std;//If you write it will report an
error'cout ' was not declared in this scope 4.pop has no return value, I mistakenly thought that the element returned was the 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;
}

Guess you like

Origin blog.csdn.net/weixin_43919570/article/details/105124032