java中跟遍历后跟遍历构造_数据结构java版 树的先根和后根遍历

本文介绍了如何在Java中实现二叉树的先根遍历和后根遍历。提供了二叉树节点的定义,并给出了计数叶子节点和非叶子节点的函数,以及判断是否为排序二叉树的函数。
摘要由CSDN通过智能技术生成

c9939646696d0e082ee17fcb364eb940.png

span name=whlm id=whlm回答:smile2me

学妹

6月13日 13:32 //BinaryTree.h

/* 二叉树的二叉链表结点定义 */

typedef char datatype;

typedef struct BiTNode

{

datatype data;

struct BiTNode * LChild , * RChild ;

} BiTNode , * BiTree ;

//数叶子结点的数目

/*

Author: WadeFelix RenV

*/

#include <stdio.h>

#include <stdlib.h>

#include "BinaryTree.h"

int countLeaf( BiTree BT )

{

if( BT == NULL ) return 0;

if( BT->LChild==NULL && BT->RChild==NULL ) return 1;

return(countLeaf(BT->LChild)+countLeaf(BT->RChild));

}

//数非叶子结点的数目

int countNotLeaf( BiTree BT )

{

if( BT == NULL ) return 0;

if( BT->LChild==NULL && BT->RChild==NULL ) return 0;

return(1+countNotLeaf(BT->LChild)+countNotLeaf(BT->RChild));

}

//判断是否是排序二叉树

#include <stdio.h>

#include <stdlib.h>

#include "BinaryTree.h"

int isPaiXu( BiTree BT )

{

if( BT == NULL )return 1;

if( BT->LChild && (BT->LChild->data > BT->data) )return 0;

if( BT->RChild && (BT->RChild->data < BT->data) )return 0;

return( isPaiXu(BT->LChild)&&isPaiXu(BT->RChild) );

}/span

◆◆

评论读取中....

请登录后再发表评论!

◆◆

修改失败,请稍后尝试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值