辅助队列层序建树+统计叶子结点数(二叉树)

#include<stdio.h>
#include<stdlib.h>
#include <stack>
#include <queue>
using namespace std;
//层次建树
typedef struct TreeNode {
	char data;
	struct TreeNode* Lchild;//左结点
	struct TreeNode* Rchild;//右结点
}TreeNode, * Tree;
typedef struct TreeQueue {
	Tree T;
	struct TreeQueue* next;

}TreeQueue, * TrQueue;//辅助队列
int Leafcount(Tree& T) {
	if (!T) {
		return 0;
	}
	else if (T->Lchild==NULL&&T->Rchild==NULL)
		return 1;
	else
		return Leafcount(T->Lchild)+Leafcount(T->Rchild);
}

int main()
{

	Tree Tr = NULL;
	TrQueue phead = NULL, ptail = NULL, pcur = NULL;//为了下次还能找到整个队列,使用pcur来代替phead模拟出队操作
	TrQueue newQ;//创建新树结点
	Tree newT;//创建新队列结点
	char c;
	while (scanf("%c", &c) != EOF) {
		if (c == '\n')
			break;
		newT = (Tree)calloc(1, sizeof(TreeNode));//calloc可以进行初始化,赋值为NULL
		newT->data = c;//创建完成一个树的节点
		newQ = (TrQueue)calloc(1, sizeof(TreeQueue));//最好定义在外部,不然后在下一个{}中就失效了
		newQ->T = newT;//创建完成一个保存树节点指针的队列结点
		if (Tr == NULL)//此时如果树还没有根结点
		{
			Tr = newT;
			phead = newQ;
			ptail = newQ;
			pcur = newQ;

		}
		else {
			ptail->next = newQ;
			ptail = ptail->next;//先尾插法加入队列尾部
			if (pcur->T->Lchild == NULL) {//先检查队头树结点的左子树是否为空,为空加入新结点
				pcur->T->Lchild = newT;
			}
			else if (pcur->T->Rchild == NULL)//若左子树不为空,再检查队头树结点的左子树是否为空,为空加入新结点
			{
				pcur->T->Rchild = newT;
				pcur = pcur->next;//队头树结点已满,进行出队操作
			}
			//else {//若两边都不为空-----你写对的话,应该不存在这种情况了

			//}
		}

	}
	printf("总叶子结点数:%d",Leafcount(Tr));

}
// ABCDEFG

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
队列层序遍历二叉树是一种广度优先搜索的算法,它的基本思路是从根节点开始,依次访问每个节点,并将每个节点的子节点按照从左到右的顺序依次加入到队列中,然后再从队列中取出一个节点,重复以上过程直到队列为空。 具体实现时,我们可以使用一个链队列来存储待访问的节点。首先将根节点入队,然后依次从队列中取出每个节点,输出该节点的值,并将该节点的左右子节点加入到队列中。重复以上过程直到队列为空即可。 下面是链队列层序遍历二叉树的代码实现: ``` typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; typedef struct QueueNode { TreeNode *node; struct QueueNode *next; } QueueNode; typedef struct { QueueNode *front; QueueNode *rear; } Queue; void initQueue(Queue *q) { q->front = q->rear = NULL; } bool isEmpty(Queue *q) { return q->front == NULL; } void enqueue(Queue *q, TreeNode *node) { QueueNode *newNode = (QueueNode *)malloc(sizeof(QueueNode)); newNode->node = node; newNode->next = NULL; if (isEmpty(q)) { q->front = q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } } TreeNode* dequeue(Queue *q) { if (isEmpty(q)) { return NULL; } TreeNode *node = q->front->node; QueueNode *tmp = q->front; if (q->front == q->rear) { q->front = q->rear = NULL; } else { q->front = q->front->next; } free(tmp); return node; } void levelOrder(TreeNode* root) { if (root == NULL) { return; } Queue q; initQueue(&q); enqueue(&q, root); while (!isEmpty(&q)) { TreeNode *node = dequeue(&q); printf("%d ", node->val); if (node->left != NULL) { enqueue(&q, node->left); } if (node->right != NULL) { enqueue(&q, node->right); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值