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的更多相关文章

  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. mysql:数据库保存时间的类型——int和datetime的区别

    我们都知道,时间保存在数据库中,可以选择使用两种类型,一种是int,一种是datetime 那么,它们两个有什么区别呢?要怎么用呢? 现在和小仓鼠一起来探讨一下 1.int和datetime的使用区别 ...

  2. PHP:return与exit的区别

        return 虽然返回数据,并且不再往下执行,但是它会返回执行上一步的操作,所以return的只是当前function而不会影响其他function的执行:      exti 是完全将整个项 ...

  3. Selenium入门20 等待时间

    自动化过程中有的页面元素加载慢或者需要等待特定条件执行后续步骤,此时需添加等待时间: 1 time.sleep()  固定等待时间,需import time 2 webdriver隐式等待 无需引入包 ...

  4. linux系统管理命令kata练习

    磁盘管理 #打印当前目录下,各个文件大小和目录的磁盘空间占用情况. #获取硬盘被占用了多少空间,目前还剩下多少空间等信息 df -lh #查看管理磁盘分区 fdisk -l #制作文件系统 mkfs ...

  5. POJ-3009 Curling 2.0---DFS求最短路

    题目链接: https://vjudge.net/problem/POJ-3009 题目大意: 问题:打冰球.冰球可以往上下左右4个方向走,只有当冰球撞到墙时才会停下来,而墙会消失.当冰球紧贴墙时,不 ...

  6. python url库学习

    参考资料:http://cuiqingcai.com/947.html urllib,urlib2是python自带的库. urlopen(url,data,timeout),三个参数. 第一个是地址 ...

  7. 2.NBU管理NetBackup

    2管理NetBackup2.1NetBackup作业任务监视 NetBackup任务监视器可以监视备份.恢复和归档任务的状态,也可以监视NetBackup本身数据库的备份. 2.1.1Activity ...

  8. Jmeter启动错误

    错误一 1 apache-jmeter-2.13\bin>jmeter 'findstr' 不是内部或外部命令,也不是可运行的程序 或批处理文件. Not able to find Java e ...

  9. 2018.7.23 oracle中的CLOB数据类型

    Oarcle中的LOB类型 1.在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了.因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这 ...

  10. 使用Newtonsoft.json 解决 Asp.Net MVC DateTime类型数据Json格式化问题

    解决思路 众所周知,MVC中调用的微软的组件JavaScriptSerialer...,格式DateTime类型数据需要在客户端专门解. 还知道,NewtonSoft.json可以“正确”的格式化Da ...