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. 运行在 Android 系统上的完整 Linux -- Termux

    Termux  可以在安卓系统上搭建一个完整的linux 环境,类似于 cygwin 并非linux 虚拟机,整个安装包只有 几百KB 刚开始觉得这东西的命令行很难用,看了官方介绍后才发现它原来有许多 ...

  2. 51Nod 1600 Simple KMP SAM+LCT/树链剖分

    1600 Simple KMP 对于一个字符串|S|,我们定义fail[i],表示最大的x使得S[1..x]=S[i-x+1..i],满足(x<i)显然对于一个字符串,如果我们将每个0<= ...

  3. 如何在win10中安装ArcGIS10.2

    在win10中安装ArcGIS10.2,完美兼容,下面将自己在win10界面下的安装方法给大家分享一下. 工具/原料   win10环境 ArcGIS10.2安装包, 安装包地址链接: 链接: htt ...

  4. eclipse使用maven install 命令,生成war包中没有jsp/js/css的解决方法

    在pom.xml文件中添加如下11行代码就可以了. <build> <plugins> <plugin> <groupId>org.apache.mav ...

  5. VPS一键测试脚本 / 自带结果导出

    脚本命令 一下脚本可能卡住,运行时间长,建议在screen中运行. 1.秋水逸冰大佬的Bench.sh脚本 特点:用时较短,对系统测试全面,英文:但缺少国内节点测速 有趣的是,bench.sh既是脚本 ...

  6. EF问题集合

    1. 在使用数据迁移的过程中,如果手工删除了本地数据库之后,再次尝试连接被删除的数据库,会有以下提示: System.Data.SqlClient.SqlException (0x80131904): ...

  7. iOS新浪微博OAuth2.0认证代码

    #import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...

  8. JS实现Promise原理

    promise是用来解决Js中的异步问题的,js中所有的异步可从callback → promise → generator + co = async + await 其实所有的都是callback的 ...

  9. 通过ABAP程序创建透明表

    最近在解决用户账号问题的时候,需要通过ABAP程序创建透明表,查询了相关资料,总结如下. 通过ABAP程序创建透明表,主要利用了4个函数: DDIF_TABL_ACTIVATE: 激活透明表 GOX_ ...

  10. MySQL5.6基于mysql-proxy实现读写分离

    已经搭建好MySQL主从架构 10.205.22.185 #mysql-proxy 10.205.22.186 #master 10.205.22.187 #slave 1.安装mysql-proxy ...