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

树的存储方式:

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. ruby在index页面显示货币符号

    1.显示人民币符号 <td><%= number_to_currency product.price, unit: "¥" %></td> 2. ...

  2. COM接口调用,CreateDispatch失败的问题

    有一个自动化处理Office文档的程序,原本运行的很好,基于效率和UI效果的问题,改成了多线程处理,编译没问题,一运行就报错找不到Office软件. 程序中产生错误的地方就是创建COM对象失败,以前好 ...

  3. nagios监控mysql及邮件报警

    1.使用默认监控命令check_http命令+相关的参数来实现,如下: 在command.cfg添加如下关键词监控命令:check_http_word,参数解析:-I指定IP或者主机名,-u指定URL ...

  4. form表单的三个属性 action 、mothod 、 enctype。

    form_action: 表单数据提交到此页面 下面的表单拥有两个输入字段以及一个提交按钮,当提交表单时,表单数据会提交到名为 "form_action.asp" 的页面: < ...

  5. coderwarrior 查看程序大小 Code Size

    https://mcuoneclipse.com/2012/09/24/code-size-information-with-gcc-for-armkinetis/

  6. windows10安装JIRA

    windows10安装MySQL数据库 一.问题现象: cmd执行“mysql”命令,提示:ERROR 2003 (HY000): Can't connect to MySQL server on ' ...

  7. 第七周博客作业 <西北师范大学| 周安伟>

    第七周博客作业 助教博客链接:https://home.cnblogs.com/u/zaw-315/ 本周无评作业 查看了同学们提交的对实验四的附加实验项目互评.其中对博文结构,内容来评价是否符合作业 ...

  8. ERROR: 9-patch image C:\...\res\drawable\appwidget.9.png malformed. Frame pixels must be either solid or transparent (not intermediate alphas).

    this is the problem with latest adt that is 20.0.3. you can instead rename the *.9.png to *.png and ...

  9. python zlib ,zlib 压缩流

    zlib 字符串:使用zlib.compress可以压缩字符串.使用zlib.decompress可以解压字符串. 数据流:压缩:compressobj,解压:decompressobj       ...

  10. 2018年 js 简易控制滚动条滚动的简单方法

    首先是es2015 的新api Element.scrollIntoView() // 滚动到最上方 等同于 dom.scrollIntoView(true) Element.scrollIntoVi ...