一种新的不使用左右子树递归进行树高计算的方法,使用层次遍历

树的存储方式:

1.本题提供的一种思路:

使用(邻接表的思想)二维数组(vector[n])表示树,横坐标表示 父节点,每一行表示孩子。

能够很轻松的使用dfs进行遍历

优点:

只需要知道输入的父和子的值,不需要清楚整个树的结构,

能够方便的使用深搜遍历,计算树高;dfs(root,high);

缺点:

占空间太大,提前要知道节点数的上限

2.双亲表示法

每个节点只有一个指向父节点的指针,

优点:

能够方便的从叶节点往回找,空间小

缺点:

叶节点不好找,可以用一个数组把所有指向叶节点的指针都存起来;

3.孩子兄弟表示法

用于多叉树,以及多棵树合并

对于输出的控制,有时候要求最后一项之后不能有空格

解决办法:在循环外先输出第一行,然后接着再循环

或者每次输出前都判断一下是不是最后一项,再输出

1004 Counting Leaves 对于树的存储方式的回顾的更多相关文章

  1. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  2. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  3. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  4. PAT甲1004 Counting Leaves【dfs】

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  5. 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 ...

  6. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. 1004 Counting Leaves ——PAT甲级真题

    1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...

  8. php数据结构课程---5、树(树的 存储方式 有哪些)

    php数据结构课程---5.树(树的 存储方式 有哪些) 一.总结 一句话总结: 双亲表示法:data parent:$tree[1] = ["B",0]; 孩子表示法:data ...

  9. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

随机推荐

  1. maven项目(转)

    我记得在搞懂maven之前看了几次重复的maven的教学视频.不知道是自己悟性太低还是怎么滴,就是搞不清楚,现在弄清楚了,基本上入门了.写该篇博文,就是为了帮助那些和我一样对于maven迷迷糊糊的人. ...

  2. 爬虫之scrapy扩展

    针对pipelines的扩展 from scrapy.exceptions import DropItem class CustomPipeline(object): def __init__(sel ...

  3. 云笔记项目-Spring事务学习-传播REQUIRES_NEW

    接下来测试事务传播的REQUIRES_NEW. Service层 Service层代码在这里不展示了,主要将EMPService1Impl类中的方法事务传播属性设置为REQUIRED,EMPServi ...

  4. Django继承AbstractUser新建UserInfor Model时出现fields.E304错误

    错误详情: SystemCheckError: System check identified some issues: ERRORS:app01.UserInfo.groups: (fields.E ...

  5. MyCP -tx -xt 功能的Java实现

    MyCP -tx -xt 功能的Java实现 功能简介 java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容为十进制数字)转化为二进制文件 java MyCP -xt ...

  6. Java I/O - 对象的输入输出与序列化

    先说概念: 一.相关概念 序列化是Java提供的一种将对象写入到输出流.并在之后将其读回的机制. 序列化:把内存中的java对象转换成与平台无关的二进制字节序列,以便永久保存在磁盘上或通过网络进行传输 ...

  7. C#入门课程之基础认识

    命名规则: 注意变量名的第一个字符必须是字母.下划线.以及@字符 字面值: 字符串字面值: 用Unicode表示一个字符方式:\uxxxx,其中xxxx表示4位的十六进制数,下面两种表示方式一致: u ...

  8. MySQL主备

    服务器基本环境: 两台centos6.4.iptables  diabled .selinux  disabled 两台的hosts解析 #yum install mysql -y  //这个一定要装 ...

  9. 监控服务器配置(四)-----OracleDb_exporter安装配置

    1.下载oracle客户端安装包(linux版)到 /opt/minitor/oracleDb . 下载地址:https://download.csdn.net/download/a155657721 ...

  10. Lonsdor K518ISE programs 2005 Ford Focus key in two minutes

    A quick demonstration of Lonsdor K518ISE programming key for 2005 Ford Focus in two minutes. And for ...