zoukankan      html  css  js  c++  java
  • PTA 中序输出叶子结点

    6-8 中序输出叶子结点 (10 分)
     

    本题要求实现一个函数,按照中序遍历的顺序输出给定二叉树的叶结点。

    函数接口定义:

    
    void InorderPrintLeaves( BiTree T);
    

    T是二叉树树根指针,InorderPrintLeaves按照中序遍历的顺序输出给定二叉树T的叶结点,格式为一个空格跟着一个字符。

    其中BiTree结构定义如下:

    typedef struct BiTNode
    {
    	ElemType data;
    	struct BiTNode *lchild,*rchild;
    }BiTNode,*BiTree;
    

    裁判测试程序样例:

    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef char ElemType;
    typedef struct BiTNode
    {
    	ElemType data;
    	struct BiTNode *lchild,*rchild;
    }BiTNode,*BiTree;
    
    BiTree Create();/* 细节在此不表 */
    void InorderPrintLeaves( BiTree T);
    
    int main()
    {
    	BiTree T = Create();
    	printf("Leaf nodes are:");
    	InorderPrintLeaves(T);
    	return 0;
    }
    /* 你的代码将被嵌在这里 */
    

    输出样例(对于图中给出的树):

    二叉树.png

    Leaf nodes are: F G C

    void InorderPrintLeaves( BiTree T){
        if(T==NULL)
            return;
        InorderPrintLeaves(T->lchild);
        if(T->lchild==NULL&&T->rchild==NULL)
            printf(" %c",T->data);
        InorderPrintLeaves(T->rchild);
    }
  • 相关阅读:
    【git】之常用命令
    VueJs
    如何做技术选型
    用户登录验证注意事项
    npm xss
    express-session
    ejs、jade和dust
    XMLHttpRequest.responseType
    JS对象字面量
    安装Angulr CLI
  • 原文地址:https://www.cnblogs.com/DirWang/p/11930006.html
Copyright © 2011-2022 走看看