《Cracking the Coding Interview》——第4章:树和图——题目4
2014-03-19 03:40
题目:给定一棵二叉树,把每一层的节点串成一个链表,最终返回一个链表数组。
解法:前序遍历,遍历的同时向各个链表里添加节点。水平遍历好像还不如前序遍历来得方便。
代码:
- // 4.4 Level order traversal
- #include <cstdio>
- #include <vector>
- using namespace std;
- struct TreeNode {
- int val;
- TreeNode *left;
- TreeNode *right;
- TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr) {};
- };
- struct ListNode {
- int val;
- ListNode *next;
- ListNode(int _val = ): val(_val), next(nullptr) {};
- };
- void consructBSTFromSortedArray(vector<int> &v, int left, int right, TreeNode *&root)
- {
- if (left > right) {
- root = nullptr;
- } else {
- int mid = (left + right + ) / ;
- root = new TreeNode(v[mid]);
- consructBSTFromSortedArray(v, left, mid - , root->left);
- consructBSTFromSortedArray(v, mid + , right, root->right);
- }
- }
- void preorderTraversal(TreeNode *root, vector<ListNode *> &listHeads, vector<ListNode *> &listTails, int depth)
- {
- if (root == nullptr) {
- printf("# ");
- } else {
- while ((int)listHeads.size() < depth) {
- listHeads.push_back(nullptr);
- listTails.push_back(nullptr);
- }
- if (listHeads[depth - ] == nullptr) {
- listHeads[depth - ] = listTails[depth - ] = new ListNode(root->val);
- } else {
- listTails[depth - ]->next = new ListNode(root->val);
- listTails[depth - ] = listTails[depth - ]->next;
- }
- printf("%d ", root->val);
- preorderTraversal(root->left, listHeads, listTails, depth + );
- preorderTraversal(root->right, listHeads, listTails, depth + );
- }
- }
- void clearBinaryTree(TreeNode *&root)
- {
- if (root == nullptr) {
- return;
- } else {
- clearBinaryTree(root->left);
- clearBinaryTree(root->right);
- delete root;
- root = nullptr;
- }
- }
- void clearList(ListNode *&root)
- {
- ListNode *ptr;
- ptr = root;
- while (ptr != nullptr) {
- root = root->next;
- delete ptr;
- ptr = root;
- }
- root = nullptr;
- }
- int main()
- {
- TreeNode *root;
- int i, n;
- vector<int> v;
- vector<ListNode *> listHeads, listTails;
- ListNode *ptr;
- while (scanf("%d", &n) == && n > ) {
- for (i = ; i < n; ++i) {
- v.push_back(i + );
- }
- consructBSTFromSortedArray(v, , n - , root);
- preorderTraversal(root, listHeads, listTails, );
- printf("\n");
- for (i = ; i < (int)listHeads.size(); ++i) {
- printf("Level %d:", i + );
- ptr = listHeads[i];
- while (ptr != nullptr) {
- printf(" %d", ptr->val);
- ptr = ptr->next;
- }
- printf("\n");
- clearList(listHeads[i]);
- }
- v.clear();
- clearBinaryTree(root);
- listHeads.clear();
- listTails.clear();
- }
- return ;
- }
《Cracking the Coding Interview》——第4章:树和图——题目4的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 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 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(二)
3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...
- 《Cracking the Coding Interview》——第4章:树和图——题目9
2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...
- 《Cracking the Coding Interview》——第4章:树和图——题目8
2014-03-19 05:04 题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树.子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样. 解法:首先可以根据 ...
随机推荐
- C语言头文件怎么写?(转载)
---恢复内容开始--- c语言头文件怎么写?我一直有这样的疑问,但是也一直没去问问到底咋回事:所以今天一定要把它弄明白! 其实学会写头文件之后可以为我们省去不少事情,可以避免书写大量的重复代码,还在 ...
- Uva 10791 最小公倍数的最小和 唯一分解定理
题目链接:https://vjudge.net/contest/156903#problem/C 题意:给一个数 n ,求至少 2个正整数,使得他们的最小公倍数为 n ,而且这些数之和最小. 分析: ...
- 如何将Win7做为NTP服务器
1. 修改注册表项 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer ...
- 问题 C: B 统计程序设计基础课程学生的平均成绩
题目描述 程序设计基础课程的学生成绩出来了,老师需要统计出学生个数和平均成绩.学生信息的输入如下: 学号(num) 学生姓名(name) ...
- 深入理解HDFS的架构和原理
(一) HDFS主要是用于做什么的? HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据存储管理的基础,是基于流数据模式访问和处 ...
- 第15章 RCC—使用HSE/HSI配置时钟—零死角玩转STM32-F429系列
第15章 RCC—使用HSE/HSI配置时钟 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku. ...
- Spring Bean依赖但注入(autowired或resource)时NullPointerException(xml和annotation混用的场景下)
项目中同时使用了xml和annotation的方式管理Spring Bean 启动时候报NullPointerException,依赖注入失败! 参考: http://fly0wing.iteye.c ...
- IIS配置MIME类型
有时候我们上传的视频,如果IIS上没有配置此格式是播放不了的.这个时候需要你在IIS上添加这个类型才能播放. MIME类型 ①打开你的IIS,点你的网站 ②双击 MIME类型 ③右键-->添加 ...
- 用纯css改变默认的radio和checkbox的样式
利用css的label的伪类(::before)代替checkbox和radio效果: 优点:需要图片来调整选中前和选中后的样式,纯css搞定 缺点:兼容性,IE8以下不支持 在线例子: css改变默 ...
- LeetCode706. Design HashMap
题目 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get ...