1004 Counting Leaves 对于树的存储方式的回顾
一种新的不使用左右子树递归进行树高计算的方法,使用层次遍历
树的存储方式:
1.本题提供的一种思路:
使用(邻接表的思想)二维数组(vector[n])表示树,横坐标表示 父节点,每一行表示孩子。
能够很轻松的使用dfs进行遍历
优点:
只需要知道输入的父和子的值,不需要清楚整个树的结构,
能够方便的使用深搜遍历,计算树高;dfs(root,high);
缺点:
占空间太大,提前要知道节点数的上限
2.双亲表示法
每个节点只有一个指向父节点的指针,
优点:
能够方便的从叶节点往回找,空间小
缺点:
叶节点不好找,可以用一个数组把所有指向叶节点的指针都存起来;
3.孩子兄弟表示法
用于多叉树,以及多棵树合并
对于输出的控制,有时候要求最后一项之后不能有空格
解决办法:在循环外先输出第一行,然后接着再循环
或者每次输出前都判断一下是不是最后一项,再输出
1004 Counting Leaves 对于树的存储方式的回顾的更多相关文章
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 1004 Counting Leaves ——PAT甲级真题
1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...
- php数据结构课程---5、树(树的 存储方式 有哪些)
php数据结构课程---5.树(树的 存储方式 有哪些) 一.总结 一句话总结: 双亲表示法:data parent:$tree[1] = ["B",0]; 孩子表示法:data ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
随机推荐
- UE4 多人网络对战游戏笔记
1.给物体施加一个径向力 定义一个径向力: URadialForceComponent* RadialForceComp; 在构造函数里赋默认值: RadialForceComp = CreateDe ...
- linux下open-vswitch安装卸载操作
一. ovs 从源码编译安装: 安装依赖项: ? 1 2 3 4 5 6 7 8 9 10 11 # apt install make # apt install gcc # apt install ...
- 50代码HTML5 Canvas 3D 编辑器优雅搞定
1024程序员节刚过,手痒想实现一个html的3d编辑器,看了three.js 同时还看了网上流传已久的<<基于 HTML5 Canvas 的简易 2D 3D 编辑器>>,都觉 ...
- mysql访问视图提示:找不到视图
原因: 1.不存在 2.视图区分大小写(有的不区分) 3.权限问题
- 树的遍历——c#实现
树作为一种重要的非线性数据结构,以分支关系定义其层次结构,在客观世界中应用广泛.通过对树遍历,将树进行线性化处理,即遍历的结果是将非线性结构的树种节点排列成一个线性序列.其中,最常见的遍历方式包括先序 ...
- linux下ping不通问题的说明与解决(DNS配置丢失)
一.出现问题的原因 最近由于linux需要使用外网,发现ping不通地址,经过一番查找分析后发现是DNS服务配置丢失,在这里有两种方法可以解决该问题. 1:你可以手动修改/etc/sysconfig/ ...
- python的可变对象与不可变对象
a = 1print(id(a))def fun(a): a = 2 print(a,id(a))fun(a)print(a,id(a)) # 1#为什么这里的a的值没有改变#因为在函数里变量赋值(内 ...
- python基础之socket编程
一 客户端/服务器架构 二 osi七层 三 socket层 四 socket是什么 五 套接字发展史及分类 六 套接字工作流程 七 基于TCP的套接字 八 基于UDP的套接字 九 粘包现象 十 什么是 ...
- LeetCode Smallest Range
数据范围是3500,3500也就是说n的平方是可以接受的.这里告诉你就是有序的,也就是在提醒你可能会是一个类似于二分的算法,所以的话其实基于这两个认识的话我们就可以利用一个枚举叫二分的算法来解决这道题 ...
- 第二周博客作业 <西北师范大学| 周安伟>
一,本周助教小结 逐步开始适应助教工作,对学生发布的博客进行点评,查看学生对软件工程前期的准备情况. 二,助教本人博客 https://home.cnblogs.com/u/zaw-315/ 三,学生 ...