BFS——PTA——03-树2 List Leaves (25分)

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

#include <iostream>
#include<cstdio>
#include<new>
#include <queue>

using namespace std;
typedef struct TR
{
	int L;
	int R;
}TR;


int main()
{
	TR T[100000];
	int n;
	cin>>n;

	//建树
	for (int i = 0; i < n; ++i)
	{
		char l,r;
		cin>>l>>r;

		if(l!='-')
			T[i].L=l-'0';
		else
			T[i].L=-1;

		if(r!='-')
			T[i].R=r-'0';
		else
			T[i].R=-1;
	}

	int s =((n-1)*n)/2;
	int s1=0;
	
	for(int i= 0;i<n;i++)
	{
		if(T[i].L>0)
			s1+=T[i].L;
		
		if(T[i].R>0)
			s1+=T[i].R;
	}	
	int h =s-s1;
	
	queue<int> q;
	bool t=0;
	q.push(h);

	//利用队列来实现宽度优先搜索
	while(!q.empty())
	{
		int a = q.front();
		
		if(T[a].L==-1&&T[a].R==-1)
		{
			if(t)
				cout<<" "<<a;
			else
			{
				cout<<a;
				t=1;
			}
		}

		
		 if(T[a].L!=-1)
			q.push(T[a].L);
		
		 if(T[a].R!=-1)
			q.push(T[a].R);
	
			
		
		q.pop();
	

	}
	

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DFS算法是一种用于图遍历或遍历的算法。其核心思想是从起点开始递归地深入每一个可能的支,直到无法继续为止,然后回溯到上一个节点,继续尝试其他支。 DFS算法有两种实现方式:递归实现和非递归实现(使用栈)。 递归实现的DFS算法伪代码: ``` DFS(node): if node is None: return visit(node) for child in node.children: DFS(child) ``` 非递归实现的DFS算法伪代码: ``` DFS(node): stack = [node] while stack: node = stack.pop() if node.visited: continue visit(node) node.visited = True for child in node.children: stack.append(child) ``` 其中,visit(node)表示对节点node进行操作,例如打印节点值、记录路径等。 DFS算法的时间复杂度为O(V+E),其中V为节点数,E为边数。因为每个节点和每条边都只会被访问一次。 下面是一个例题,用DFS算法求解从起点到终点的路径(leetcode 79题): 给定一个二维网格和一个单词,找出该单词是否存在于网格中。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。 示例: ``` board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "ABCCED", 返回 true. 给定 word = "SEE", 返回 true. 给定 word = "ABCB", 返回 false. ``` 实现代码: ```python class Solution: def exist(self, board: List[List[str]], word: str) -> bool: if not board or not board[0]: return False m, n = len(board), len(board[0]) visited = [[False] * n for _ in range(m)] directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] def dfs(i, j, k): if k == len(word): return True if i < 0 or i >= m or j < 0 or j >= n or visited[i][j] or board[i][j] != word[k]: return False visited[i][j] = True for dx, dy in directions: if dfs(i+dx, j+dy, k+1): return True visited[i][j] = False return False for i in range(m): for j in range(n): if dfs(i, j, 0): return True return False ``` 时间复杂度为O(m*n*3^k),其中m、n为网格大小,k为单词长度。因为每个格子都有可能走,所以是O(m*n),而每次调用dfs函数会向四个方向递归,每个方向都不能重复走,所以是3^k。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值