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 ...
随机推荐
- C#串口数据收发数据
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- python入门(十四):面向对象(属性、方法、继承、多继承)
1.任何东西1)属性(特征:通常可以用数据来描述)(类变量和实例变量)2)可以做一些动作(方法) 类来管理对象的数据.属性:类变量和实例变量(私有变量)方法: 1)实例方法 2)类方法 ...
- 云笔记项目-MyBatis返回自增类型&堆栈对象补充理解
在云笔记项目中,讲到了MySql的自增,MyBatis查询到自增类型数据后可以设置返回到参数属性,其中学习了MySql的自增写法,堆栈对象等知识. MySql数据类型自增 建立一张Person表,其中 ...
- java api 批量数据库操作
Statement.class:executeBatch() implement interface: PreparedStatement implement class: JdbcOdbcPrepa ...
- SecureCRT连接开发板 串口传输、tftp传输
1.串口传输 使用命令:rx r是service, x是X-model模式 ①.rx 文件名 再按Enter键 ②.将需要传到板子上的文件 拖到SecureCRT里面,选择发送X-model选项 注 ...
- React-Native android 开发者记录
1.安装 安装步骤不多废话,按照官网步骤执行即可 安装完之后,react-native run-android发现报错,页面出不来 Error: Unable to resolve module `. ...
- Cannot attach medium 'D:\program\VirtualBox\VBoxGuestAdditions.iso' {}: medium is already associated with the current state of machine uuid {}返回 代码: VBOX_E_OBJECT_IN_USE (0x80BB000C)
详细的错误信息如下: Cannot attach medium 'D:\program\VirtualBox\VBoxGuestAdditions.iso' {83b35b10-8fa2-4b81-8 ...
- unity 动态更新模型透明度
RaycastHit[] hits; Vector3 normal = transform.position - target.position; hits = Physics.RaycastAll( ...
- C#使用CefSharp开源库开发Chrome 浏览器
一.介绍 这个东西我以前没有接触过,但是公司项目里面有用到这个东西,所以就顺便研究一下.今天只是做了 WinForm 的测试,有时间了在试试 WPF 是如何实现的.刚开始一塌糊涂,有点麻 ...
- CentOS如何手动增加 删除swap区
SWAP是Linux中的虚拟内存,用于扩充物理内存不足而用来存储临时数据存在的.它类似于Windows中的虚拟内存.在Windows中,只可以使用文件来当作虚拟内存.而linux可以文件或者分区来当作 ...