0
点赞
收藏
分享

微信扫一扫

数据结构之SWUSTOJ977: 统计利用先序遍历创建的二叉树中的空链域个数

小暴龙要抱抱 2022-04-17 阅读 52

题目:

代码:

#include<iostream>
using namespace std;
typedef struct BinaryTree
{
	char data;
	struct BinaryTree* leftchild;
	struct BinaryTree* rightchild;
}BT;
void BinaryTreePreCreate(BT*& root)//注意要传引用创建二叉树
{
	char a;
	cin >> a;
	if (a == '#')
		root = NULL;
	else
	{
		root = (BT*)malloc(sizeof(BT));
		root->data = a;
		BinaryTreePreCreate(root->leftchild);
		BinaryTreePreCreate(root->rightchild);
	}
}
int Count = 0;//全局变量记录空的个数
void BinaryTreeNullDomain(BT* root)
{
	if (root == NULL)
	{
		Count++;
		return;
	}
	BinaryTreeNullDomain(root->leftchild);
	BinaryTreeNullDomain(root->rightchild);
}
int main()
{
	BT* tree;
	BinaryTreePreCreate(tree);
	BinaryTreeNullDomain(tree);
	printf("%d", Count);
	return 0;
}
举报

相关推荐

二叉树的先序 中序 后序遍历

0 条评论