《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一模一样. 解法:首先可以根据 ...
随机推荐
- ubuntu linux double tab
在terminal中,输入部分指令,再按两下Tab键,可以显示以相关的指令
- vuejs使用组件的细节点
is属性 <div id='root'> <table> <tbody> <row></row> <row></row&g ...
- 剑指offer39 平衡二叉树
剑指上用了指针传递,这里用的引用传递 class Solution { public: bool IsBalanced_Solution(TreeNode* pRoot) { ; return IsB ...
- mysql如何查看错误代码具体释义?(基于perror)
mysql如何查看错误代码具体释义? 关键词:mysql错误代码,mysql错误号 perror 错误号
- OpenACC例子
timeinfo1.c代码 #include<stdio.h> #define N 100 int main() { int A[N]; #pragma acc kernels { ; i ...
- alert、confirm、prompt的区别
我们在开发一些后台管理系统的时候,经常会用到一些弹出框,今天我们一起看一下吧: alert:仅仅是一个提示的作用,通知用户,会阻线程,alert后面的代码,在用户点击确认后仍然会照常执行. confi ...
- AngularJS 控制器其他实例
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- C#中的委托是什么?事件是不是一种委托?
C#中的委托是什么? 委托可以把一个方法作为参数代入另一个方法. 委托可以理解为指向一个函数的引用. 事件是不是一种委托?事件是一种特殊的委托.
- IE 兼容模式 设置 Meta Compatible 和 Iframe 子页面的关系
背景 因为历史原因,之前很多的系统都会是 顶级页面+Iframe来加载子级页面的这种模式构件系统,而且系统都只能运行在IE6或者IE 高版本兼容模式下(IE 7模式). 随着现在的审美原来越高,脚本能 ...
- 11、SpringBoot------定时任务
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/Springboot/tree/52ef6c0c805913db1e66ed18671c322e ...