层序遍历输出叶子结点

题目:
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a “-” will be put at the position. Any pair of children are separated by a space.

Output Specification:
For each test case, print in one line all the leaves’ indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

**Sample Input:
8
1 -

0 -
2 7

5 -
4 6**
Sample Output:
4 1 5
作者: 陈越
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB

解题思路:
层序遍历:创建队列保存元素,先推入根节点,判断其是否为叶子节点,是则输出,否则推入其儿子,
最后抛出该节点,这样就完成了一次循环,到抛出最后一个结点的时候队列为空,则循环结束。

#include <iostream>
#include <deque>
using namespace std;
//用结构数组表示二叉树
struct StreeNode
{
	int left;
	int right;
}T1[10];

int biuldTree(struct StreeNode T[]);
void inoderTraversal(int );
void levelTraversal(int);
int  main()
{
	int R1 = 0;
	R1 = biuldTree(T1);//建造二叉树
	//inoderTraversal(R1);//中序遍历
	levelTraversal(R1);//层序遍历
	system("pause");
	return 0;
}

void inoderTraversal(int r1)
{
	int flag = 0;
	if (r1 == -1)return;
	inoderTraversal(T1[r1].left);
	if (T1[r1].left == -1 && T1[r1].right == -1){ flag ? cout << r1 << " " : cout << r1; flag++; }
	inoderTraversal(T1[r1].right);
}

void levelTraversal(int r1)
{
	if (r1==-1)
		return;
	int flag = 0;
	deque<int>d;//建立双端队列
	d.push_back(r1);//将根节点添加入队列
	while (!d.empty())
	{
		if (T1[d[0]].left == -1 && T1[d[0]].right == -1)//如果膝下无子女则为叶子节点输出
		{
			flag ? cout << " " << d[0] : cout << d[0]; flag++;
		}
		
		if (T1[d[0]].left != -1)d.push_back(T1[d[0]].left);//有儿子的话把儿子的编号推入队列
		if (T1[d[0]].right != -1)d.push_back(T1[d[0]].right);
		d.pop_front();//抛出首元素
	}
}
int biuldTree(struct StreeNode T[])
{
	int N, Root = -1;//当N为0即输入空树的时候,根节点的编号root应该为-1
	char cl, cr;
	int check[10];//识别根节点用的数组
	cin >> N;
	if (N)
	{

		for (int &i : check)	i = 0;	//全部元素初始化为0
		for (int i = 0; i < N; i++)
		{
			cin  >> cl >> cr;

			if (cl != '-')//左边有儿子
			{
				T[i].left = cl - '0';//left是int,cl是char,char-char==int(ascll码值的差);
				check[T[i].left] = 1; //当你是别人的儿子的时候,你的check变为1
			}
			else T[i].left = -1;//左边没儿子,就把左儿子设为-1`在这里插入代码片`
			if (cr != '-')
			{
				T[i].right = cr - '0';
				check[T[i].right] = 1;
			}
			else T[i].right = -1;
		}
	}
	else return -1;//与根节点初始化为-1的目的一样,当输入空树的时候保证根节点编号为-1,两者写其中一个就行。
	for (int i = 0; i < N; i++)//是别的儿子的check都是1.还是0的那个就是根节点
	{
		if (!check[i])
		{
			Root = i;
			break;
		}
	}
	return Root;//返回根节点编号
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会根据您的要求进行回答。针对您的问题,可以使用层序遍历的方法进行遍历,并统计所有子节点的值的和。具体步骤如下: 1. 定义一个队列,将根节点入队。 2. 当队列不为空时,进行以下操作: a. 取出队首元素,并判断其左右子树是否为空,若不为空则将其左右子树入队。 b. 判断当前节点是否为子节点,若是则将其值加入子节点值的和中。 3. 遍历结束后,返回子节点值的和。 以下是 C++ 代码实现: ```c++ #include <iostream> #include <queue> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; int sumOfLeaf(TreeNode* root) { int sum = 0; if (root == nullptr) { return sum; } queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* cur = q.front(); q.pop(); if (cur->left != nullptr) { q.push(cur->left); } if (cur->right != nullptr) { q.push(cur->right); } if (cur->left == nullptr && cur->right == nullptr) { sum += cur->val; } } return sum; } int main() { TreeNode* root = new TreeNode(1); root->left = new TreeNode(2); root->right = new TreeNode(3); root->left->left = new TreeNode(4); root->left->right = new TreeNode(5); root->right->left = new TreeNode(6); root->right->right = new TreeNode(7); cout << sumOfLeaf(root) << endl; // 输出 22 return 0; } ``` 希望我的回答能够帮到您,如果您还有其他问题,欢迎随时提出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值