The first review of the finale: the basic operation of the binary tree (2)

Sequence traversal of binary tree:

http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2711/pid/3344

This question needs to contact the previous one: stack and queue

Save time and only output functions

void cengxu(struct tree*root)
{     int in,out;     struct tree*temp[55];     in=0;     out=0;     temp[in]=root;     if(in>out)     {         if(temp[out] )//Ensure that the output is counted, be careful not to use in, because if in stops at NULL, then the remaining out++ will not come         {             printf("%c",temp[out]->data);             temp[in++]=temp [out]->l;//The left son enters             temp[in++]=temp[out]->r;//The right son enters         }         out++;     } }















Let's debug manually, or build the simplest tree abc, because this book structure pointer array, first store the root of the tree, output a under the condition of ensuring in>out, and then put the left and right children into the queue, and then The same

Counting the number of leaves means counting the bottom child (both left and right children below him are empty)

Refer to the article of the boss: https://blog.csdn.net/zhangkongzhongyun/article/details/38037059

http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2711/pid/3342

void searchleaf(struct node*root)
{
    if(root)
    {
        if(root->l==NULL&&root->r=NULL)
        {
            count++;
        }
        searchleaf(root->l);
        searchleaf(root->r);
    }
    return count;
}

Manual debugging: To build an abc tree, first searchleaf(root->l)->then count++ because the bottom of b is NULL, and then search directly (root->r), and find that the following is NULL, so count is added twice, the same is true The same goes for other more complex trees

 

Guess you like

Origin blog.csdn.net/weixin_44067773/article/details/87867662