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. 笨办法学Python(二)

    习题 2: 注释和井号 程序里的注释是很重要的.它们可以用自然语言告诉你某段代码的功能是什么.在你想要临时移除一段代码时,你还可以用注解的方式将这段代码临时禁用.接下来的练习将让你学会注释: #-- ...

  2. IOS 解析XML数据

    ●  什么是XML ●  全称是Extensible Markup Language,译作“可扩展标记语言” ●  跟JSON一样,也是常用的一种用于交互的数据格式 ●  一般也叫XML文档(XML ...

  3. soap使用xml调用webapi后返回xml信息进行JSON转换处理,以顺丰查询接口为例

    expressUrl = string.Format(可以卸载配置文件的域名URL + "/bsp-oisp/ws/expressService"); StringBuilder ...

  4. Prim算法求最大权,POJ(2485)

    题目链接:http://poj.org/problem?id=2485 解题报告: 这里有一点要注意的是,第一个点时,dis数组还没有初始化,还全部为inf.第一次来到更新权时,才把邻接矩阵的数据存到 ...

  5. BZOJ 2824: [AHOI2012]铁盘整理

    BZOJ 2824: [AHOI2012]铁盘整理 标签(空格分隔): OI-BZOJ OI-搜索 Time Limit: 10 Sec Memory Limit: 128 MB Descriptio ...

  6. Linux空间PHP开发环境小白教程(LAMP)

    租了一个云服务器, 但是只有linux系统,没有php开发环境, 只好自己摸索着一步一步安装啦. 本教程来自自学IT创E老师的Linux教程,想详细了解的可以去论坛找. 一.使用PUTTY登录服务器 ...

  7. 2018.8.26 Spring自学如门

    什么是Spring? Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE De ...

  8. 第26章 FMC—扩展外部SDRAM—零死角玩转STM32-F429系列

    第26章     FMC—扩展外部SDRAM 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/ ...

  9. java 基础词汇 必须 第九天

    Collection 集合 List 列表集合 Set 不重复集合 Linked 链表 Vector 线程安全集合 Hash 哈希值 tree 树型结构 Map 键值对集合 add 增加 remove ...

  10. eclipse环境Dynamic web module version 3.1版本的进步,简化Dynamic web object 中Servlet类的配置,不用web.xml配置<Servlet>

    eclipse环境Dynamic web module version 3.1版本之前,Dynamic web object 中Servlet类的配置,要在web.xml 配置<Servlet& ...