首页 > 代码库 > 4-11 先序输出叶节点

4-11 先序输出叶节点

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #822d0f } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #c81b13 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #c42275 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #539aa4 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #1e9421 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #294c50 } span.s1 { } span.s2 { font: 14.0px "PingFang SC" } span.s3 { color: #c81b13 } span.s4 { color: #822d0f } span.s5 { color: #000000 } span.s6 { color: #703daa } span.s7 { color: #0435ff } span.s8 { color: #c42275 } span.s9 { color: #539aa4 } span.s10 { color: #1e9421 } span.s11 { font: 14.0px "PingFang SC"; color: #1e9421 } span.s12 { font: 14.0px Menlo; color: #000000 } span.s13 { font: 14.0px Menlo; color: #703daa } span.s14 { font: 14.0px Menlo } span.s15 { color: #3e1e81 } span.s16 { color: #78492a } span.s17 { color: #294c50 }</style>

4-11 先序输出叶结点   (15分)

 

本题要求按照先序遍历的顺序输出给定二叉树的叶结点。

函数PreorderPrintLeaves应按照先序遍历的顺序输出给定二叉树BT的叶结点,格式为一个空格跟着一个字符。

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

BinTree CreatBinTree(); /* 实现细节忽略 */
void PreorderPrintLeaves( BinTree BT );

int main()
{
    BinTree BT = CreatBinTree();
    printf("Leaf nodes are:");
    PreorderPrintLeaves(BT);
    printf("\n");

    return 0;
}
/* 你的代码将被嵌在这里 */

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

技术分享

Leaf nodes are: D E H I

 

 

 

 

 1 //  4-11 先序输出叶结点
 2 //
 3 //  Created by Haoyu Guo on 05/02/2017.
 4 //  Copyright ? 2017 Haoyu Guo. All rights reserved.
 5 //
 6 #include <stdio.h>
 7 #include<iostream>
 8 #include <stdlib.h>
 9 using namespace std;
10 #define OVERFLOW -2
11 typedef char ElementType;
12 typedef struct TNode *Position;
13 typedef Position BinTree;
14 struct TNode{
15     ElementType Data;
16     BinTree Left;
17     BinTree Right;
18 };
19 int CreatBinTree(BinTree &T)//创建二叉树
20 {
21     char ch;//按先序的方式输入
22     cin >> ch;//递归中自然带着一些重复,就不需要循环了
23     if (ch == #)  T=NULL;
24     else {
25         if (!(T = (BinTree)malloc(sizeof(TNode)))) exit(OVERFLOW);
26         T->Data =http://www.mamicode.com/ ch;
27         CreatBinTree(T->Left);
28         CreatBinTree(T->Right);
29     }
30     return 0;
31 }
32 
33 void PreorderPrintLeaves( BinTree BT );
34 
35 int main()
36 {
37     BinTree BT;
38     CreatBinTree(BT);//创建一个
39     printf("Leaf nodes are:");
40     PreorderPrintLeaves(BT);
41     printf("\n");
42     return 0;
43 }
44 /* 你的代码将被嵌在这里 */
45 void PreorderPrintLeaves( BinTree BT)//先序输出
46 {
47     if(BT==NULL) return;
48     else{
49     if(BT->Left==NULL&&BT->Right==NULL)
50        printf(" %c",BT->Data);
51     PreorderPrintLeaves(BT->Left);
52     PreorderPrintLeaves(BT->Right);
53   }
54 }

 

4-11 先序输出叶节点