03-树2. List Leaves (25) 二叉树的层序遍历
03-树2. List Leaves (25)
题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%912
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
- 题目大意: 通过输入节点数以及每个节点的左儿子和右儿子,从上到下打印出叶节点。
题目关键:要理解输入的第几行就是代表该节点的值为几。例如样例输入中第0行的1 -代表值为0的节点左孩子的值为1,即指向第1行,右孩子为空(-1)
树模型如下:
- 代码如下:
- #include <cstdio>
- #include <cctype>
- #define N 10
- typedef struct Node
- {
- int data, left, right;
- } TreeNode;
- TreeNode node[N];
- TreeNode Queue[N]; //数组实现队列
- int first = -, last = -;
- void Push(TreeNode tn);
- TreeNode Pop();
- void printLeaves(int root, int n);
- int charToInt(char ch);
- int main()
- {
- int n;
- bool isRoot[N];
- int root;
- scanf("%d\n", &n);
- for (int i = ; i < n; i++)
- isRoot[i] = ;
- for (int i = ; i < n; i++)
- {
- char cLeft, cRight;
- scanf("%c %c", &cLeft, &cRight);
- getchar(); //读取缓存区的回车符
- node[i].left = charToInt(cLeft);
- node[i].right = charToInt(cRight);
- node[i].data = i;
- //一个节点的左孩子和右孩子一定不是根节点
- if (node[i].left != -)
- isRoot[node[i].left] = ;
- if (node[i].right != -)
- isRoot[node[i].right] = ;
- }
- //找到根节点
- for (int i = ; i < n; i++)
- {
- if (isRoot[i])
- {
- root = i;
- break;
- }
- }
- printLeaves(root, n);
- return ;
- }
- void Push(TreeNode treeNode)
- {
- Queue[++last] = treeNode;
- }
- TreeNode Pop()
- {
- return Queue[++first];
- }
- //层序遍历树节点并打印出叶节点:队列实现
- void printLeaves(int root, int n)
- {
- int leaves[N];
- int k = ;
- Push(node[root]);
- for (int i = ; i < n; i++)
- {
- TreeNode tn = Pop();
- //左孩子和右孩子都不存在时,将叶节点的值保存到数组中,便于格式化打印
- if (tn.left == - && tn.right == -)
- leaves[k++] = tn.data;
- if (tn.left != -)
- Push(node[tn.left]);
- if (tn.right != -)
- Push(node[tn.right]);
- }
- for (int i = ; i < k-; i++)
- printf("%d ", leaves[i]);
- printf("%d\n", leaves[k-]);
- }
- int charToInt(char ch)
- {
- if (isdigit(ch))
- return ch - '';
- else
- return -;
- }
03-树2. List Leaves (25) 二叉树的层序遍历的更多相关文章
- 二叉树的层序遍历 BFS
二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容. 问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行. #include <iostream> #include &l ...
- 五三想休息,今天还学习,图解二叉树的层序遍历BFS(广度优先)模板,附面试题题解
壹 ❀ 引 我在从JS执行栈角度图解递归以及二叉树的前.中.后遍历的底层差异一文中,从一个最基本的数组遍历引出递归,在掌握递归的书写规则后,又从JS执行栈角度解释了二叉树三种深度优先(前序.中序后序) ...
- Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 剑指offer 二叉树的层序遍历
剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...
- leetcode之二叉树的层序遍历
1.题目描述 2.题目分析 二叉树的层序遍历主要算法思想是使用 队列这一数据结构实现,这个数据结构多应用在和 图相关的算法.例如图的广度优先遍历就可以使用队列的方法实现.本题的关键在于如何识别出一层已 ...
- LeetCode 102. 二叉树的层序遍历 | Python
102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...
- 刷题-力扣-107. 二叉树的层序遍历 II
107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...
- leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
随机推荐
- [SHELL]查看端口,文件,服务关系的四个命令netstat,lsof,fuser,nmap
一,netstat (1)简介 netstat主要是用来打印系统网络的状态信息,当输入netstat后,输出如下: 可以看出,netstat的输出分为两个部分组成: 一个是Active Interne ...
- Select 、Poll 和 Epoll
作用 Epoll 和 Select 的作用都是为了多I/O同步复用的问题,利用Epoll.Poll或Select函数指定内核监听多个I/O的读.写.异常事件,避免每一个I/O都指定一个处理线程,导致开 ...
- Python3【基础】-表达式与运算符
一.什么是表达式? 1+2*3就是一个表达式,这里的加号和乘号叫做运算符,1.2.3叫做操作数.1+2*3计算的结果是7,计算结果可以存到一个变量中,即:res = 1 + 2 * 3. 所谓的表达式 ...
- PSP阶段和WBS
项目:PSP Daily 详情请见项目功能说明书 PSP2.1 Personal Software Process Stages 预估耗时长 Planning 计划 · Estimate · 开发 ...
- Ubuntu环境下No module named '_tkinter'错误的解决
在Ubuntu环境下运行下面代码: import matplotlib as plt 出现以下错误: No module named '_tkinter' 解决方法: sudo apt-get ins ...
- [CF] Sasha and One More Name
题目大意 就是给一个回文串,然后进行k次分割,产生k+1个字符子串,通过重新组合这k+1个字符字串,是否会出现新的不同的回文串,且最少需要分割几段.无法产生新的回文串则输出"Impossib ...
- web压力测试_(收集)
作者:ZeldaZzz链接:http://www.zhihu.com/question/19867883/answer/89775858来源:知乎著作权归作者所有,转载请联系作者获得授权. 一个完整的 ...
- lintcode-411-格雷编码
411-格雷编码 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个二进制的差异. 给定一个非负整数 n ,表示该代码中所有二进制的总数,请找出其格雷编码顺序.一个格雷编码顺序必须以 0 ...
- centos 6 编译emacs-24.5
yum install `yum deplist emacs | grep provider | awk -F: '{print $2}' | awk '{print $1}' | xargs` yu ...
- 结对编程--fault,error,failure的程序设计
一.结对编程内容: 1.不能触发Fault. 2.触发Fault,但是不触发Error. 3.触发Error,但不触发Failure. 二.结对编程人员 1.周浩,周宗耀 2.结对截图: 三.结对项目 ...