// 树的遍历.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct BTNode{
int data;
struct BTNode *lchild, *rchild;
};
typedef struct BTNode BTnode;
typedef struct BTNode* BiTree;
void preOrder(BTNode *root){
if (root == NULL){
return;
}
printf("%d",root->data);
//遍历左子树
preOrder(root->lchild);
//遍历右子树
preOrder(root->rchild);
}
void inOrder(BTNode *root){//中序遍历
if (root == NULL){
return;
}
//遍历左子树
preOrder(root->lchild);
printf("%d", root->data);
//遍历右子树
preOrder(root->rchild);
}
void postOrder(BTNode *root){//后序遍历
if (root == NULL){
return;
}
//遍历左子树
preOrder(root->lchild);
//遍历右子树
preOrder(root->rchild);
printf("%d", root->data);
}
int sum;
void cntLeaf(BTNode *root){//求叶子结点的个数
if (root != NULL){
if (root->lchild == NULL&&root->rchild == NULL){
sum++
求树的叶子结点的个数
最新推荐文章于 2021-03-20 11:26:43 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)