2014-03-19 03:40

题目:给定一棵二叉树,把每一层的节点串成一个链表,最终返回一个链表数组。

解法:前序遍历,遍历的同时向各个链表里添加节点。水平遍历好像还不如前序遍历来得方便。

代码:

  1. // 4.4 Level order traversal
  2. #include <cstdio>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. struct TreeNode {
  7. int val;
  8. TreeNode *left;
  9. TreeNode *right;
  10.  
  11. TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr) {};
  12. };
  13.  
  14. struct ListNode {
  15. int val;
  16. ListNode *next;
  17.  
  18. ListNode(int _val = ): val(_val), next(nullptr) {};
  19. };
  20.  
  21. void consructBSTFromSortedArray(vector<int> &v, int left, int right, TreeNode *&root)
  22. {
  23. if (left > right) {
  24. root = nullptr;
  25. } else {
  26. int mid = (left + right + ) / ;
  27. root = new TreeNode(v[mid]);
  28. consructBSTFromSortedArray(v, left, mid - , root->left);
  29. consructBSTFromSortedArray(v, mid + , right, root->right);
  30. }
  31. }
  32.  
  33. void preorderTraversal(TreeNode *root, vector<ListNode *> &listHeads, vector<ListNode *> &listTails, int depth)
  34. {
  35. if (root == nullptr) {
  36. printf("# ");
  37. } else {
  38. while ((int)listHeads.size() < depth) {
  39. listHeads.push_back(nullptr);
  40. listTails.push_back(nullptr);
  41. }
  42.  
  43. if (listHeads[depth - ] == nullptr) {
  44. listHeads[depth - ] = listTails[depth - ] = new ListNode(root->val);
  45. } else {
  46. listTails[depth - ]->next = new ListNode(root->val);
  47. listTails[depth - ] = listTails[depth - ]->next;
  48. }
  49.  
  50. printf("%d ", root->val);
  51. preorderTraversal(root->left, listHeads, listTails, depth + );
  52. preorderTraversal(root->right, listHeads, listTails, depth + );
  53. }
  54. }
  55.  
  56. void clearBinaryTree(TreeNode *&root)
  57. {
  58. if (root == nullptr) {
  59. return;
  60. } else {
  61. clearBinaryTree(root->left);
  62. clearBinaryTree(root->right);
  63. delete root;
  64. root = nullptr;
  65. }
  66. }
  67.  
  68. void clearList(ListNode *&root)
  69. {
  70. ListNode *ptr;
  71.  
  72. ptr = root;
  73. while (ptr != nullptr) {
  74. root = root->next;
  75. delete ptr;
  76. ptr = root;
  77. }
  78. root = nullptr;
  79. }
  80.  
  81. int main()
  82. {
  83. TreeNode *root;
  84. int i, n;
  85. vector<int> v;
  86. vector<ListNode *> listHeads, listTails;
  87. ListNode *ptr;
  88.  
  89. while (scanf("%d", &n) == && n > ) {
  90. for (i = ; i < n; ++i) {
  91. v.push_back(i + );
  92. }
  93.  
  94. consructBSTFromSortedArray(v, , n - , root);
  95. preorderTraversal(root, listHeads, listTails, );
  96. printf("\n");
  97.  
  98. for (i = ; i < (int)listHeads.size(); ++i) {
  99. printf("Level %d:", i + );
  100. ptr = listHeads[i];
  101. while (ptr != nullptr) {
  102. printf(" %d", ptr->val);
  103. ptr = ptr->next;
  104. }
  105. printf("\n");
  106. clearList(listHeads[i]);
  107. }
  108.  
  109. v.clear();
  110. clearBinaryTree(root);
  111. listHeads.clear();
  112. listTails.clear();
  113. }
  114.  
  115. return ;
  116. }

《Cracking the Coding Interview》——第4章:树和图——题目4的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. Cracking the Coding Interview 150题(二)

    3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...

  9. 《Cracking the Coding Interview》——第4章:树和图——题目9

    2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...

  10. 《Cracking the Coding Interview》——第4章:树和图——题目8

    2014-03-19 05:04 题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树.子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样. 解法:首先可以根据 ...

随机推荐

  1. C语言头文件怎么写?(转载)

    ---恢复内容开始--- c语言头文件怎么写?我一直有这样的疑问,但是也一直没去问问到底咋回事:所以今天一定要把它弄明白! 其实学会写头文件之后可以为我们省去不少事情,可以避免书写大量的重复代码,还在 ...

  2. Uva 10791 最小公倍数的最小和 唯一分解定理

    题目链接:https://vjudge.net/contest/156903#problem/C 题意:给一个数 n ,求至少 2个正整数,使得他们的最小公倍数为 n ,而且这些数之和最小. 分析: ...

  3. 如何将Win7做为NTP服务器

    1. 修改注册表项    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer   ...

  4. 问题 C: B 统计程序设计基础课程学生的平均成绩

    题目描述 程序设计基础课程的学生成绩出来了,老师需要统计出学生个数和平均成绩.学生信息的输入如下: 学号(num)                     学生姓名(name)            ...

  5. 深入理解HDFS的架构和原理

    (一) HDFS主要是用于做什么的? HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据存储管理的基础,是基于流数据模式访问和处 ...

  6. 第15章 RCC—使用HSE/HSI配置时钟—零死角玩转STM32-F429系列

    第15章     RCC—使用HSE/HSI配置时钟 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku. ...

  7. Spring Bean依赖但注入(autowired或resource)时NullPointerException(xml和annotation混用的场景下)

    项目中同时使用了xml和annotation的方式管理Spring Bean 启动时候报NullPointerException,依赖注入失败! 参考: http://fly0wing.iteye.c ...

  8. IIS配置MIME类型

    有时候我们上传的视频,如果IIS上没有配置此格式是播放不了的.这个时候需要你在IIS上添加这个类型才能播放. MIME类型 ①打开你的IIS,点你的网站 ②双击 MIME类型 ③右键-->添加 ...

  9. 用纯css改变默认的radio和checkbox的样式

    利用css的label的伪类(::before)代替checkbox和radio效果: 优点:需要图片来调整选中前和选中后的样式,纯css搞定 缺点:兼容性,IE8以下不支持 在线例子: css改变默 ...

  10. LeetCode706. Design HashMap

    题目  不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get ...