《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一模一样. 解法:首先可以根据 ...
随机推荐
- COGS 2075. [ZLXOI2015][异次元圣战III]ZLX的陨落
★★☆ 输入文件:ThefallingofZLX.in 输出文件:ThefallingofZLX.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 正当革命如火如 ...
- Html + JS : 点击对应的按钮,进行选择是隐藏还是显示(用户回复功能)
例如: 当我点击按钮1时,点击第一下进行显示This is comment 01,点击第二下隐藏This is comment 01 当我点击按钮2时,点击第一下进行显示This is comment ...
- TP5.1:数据库的增删改查操作(基于面向对象操作)
我们现实中对数据库的增删改查操作,都是使用模型类进行操作的(表名::),也就是面向对象操作,只有底层的代码用的是数据库操作(Db::table('表名')) 下面我将贴出模型类进行的增删改查操作,通过 ...
- Computer Science: the Big Picture
1.课程PPTMIT OpenCourseWarehttp://ocw.mit.edu/courses/; Courses Stanfordhttp://cs.stanford.edu/course ...
- node 上的cookie的签名和解签名
cookie签名的原因是防止别人篡改cookie原本的值,如果这个过程中cookie被改变的话,就会在unsign方法返回false 代码: var cookie = require("co ...
- httpclient使用head添加cookie
最近在使用接口时候,我使用get请求时,需要携带登录态,所以在get请求的时候我需要在head里面把cookie给加上,添加方式get和post完全不一样 Post方式添加cookie httpPos ...
- flex布局-常用布局
在使用flex布局,老是需要去查资料,很多常用的,知道大概,可还是需要去过一遍,这里记录一下几个常用的flex布局 一个div,内容垂直居中 html <div className='topHe ...
- 2018年第九届蓝桥杯【C++省赛B组】第三题 乘积尾零
如下的10行数据,每行有10个整数,请你求出它们的乘积的末尾有多少个零?5650 4542 3554 473 946 4114 3871 9073 90 43292758 7949 6113 5659 ...
- Oracle 使用Nid 修改数据库的DBID 和 Database Name
How to Change the DBID, DBNAME Using NID Utility (Doc ID 863800.1) Changing the DBID and Database Na ...
- 11_1_GUI
11_1_GUI 1. AWT AWT(Abstract Window Toolkit)包括了很多类和接口,用于Java Application的GUI(Graphics User Interface ...