《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15
题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成。
解法:Leetcode上也有,递归解法。
代码:
// 17.13 Flatten a binary search tree into a doubly linked list by inorder traversal order.
// Use postorder traversal to do the flattening job.
#include <cstdio>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr) {};
}; void flatten(TreeNode *&root, TreeNode *&left_most, TreeNode *&right_most)
{
if (root == nullptr) {
left_most = right_most = nullptr;
return;
} TreeNode *ll, *lr, *rl, *rr;
if (root->left != nullptr) {
flatten(root->left, ll, lr);
root->left = lr;
lr->right = root;
} else {
ll = lr = root;
} if (root->right != nullptr) {
flatten(root->right, rl, rr);
root->right = rl;
rl->left = root;
} else {
rl = rr = root;
} left_most = ll;
right_most = rr;
} void constructBinaryTree(TreeNode *&root)
{
int val; if (scanf("%d", &val) != ) {
root = nullptr;
} else if (val == ) {
root = nullptr;
} else {
root = new TreeNode(val);
constructBinaryTree(root->left);
constructBinaryTree(root->right);
}
} void deleteList(TreeNode *&head)
{
TreeNode *ptr; while (head != nullptr) {
ptr = head;
head = head->right;
delete ptr;
}
} int main()
{
TreeNode *root;
TreeNode *left_most, *right_most;
TreeNode *head;
TreeNode *ptr; while (true) {
constructBinaryTree(root);
if (root == nullptr) {
break;
}
flatten(root, left_most, right_most);
head = left_most;
for (ptr = head; ptr != nullptr; ptr = ptr->right) {
printf("%d ", ptr->val);
}
putchar('\n');
deleteList(head);
} return ;
}
《Cracking the Coding Interview》——第17章:普通题——题目13的更多相关文章
- 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目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目14
2014-04-29 00:20 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...
随机推荐
- ARM实验3 ——串口实验
uart串口实验 实验内容: 编写UART模块程序,通过串口将信息打印到终端. 实验目的: 熟悉开发环境的使用. 掌握exynos4412处理器的UART功能. 实验平台: FS4412开发板,ecl ...
- QR分解与最小二乘(转载自AndyJee)
转载网址:http://www.cnblogs.com/AndyJee/p/3846455.html 主要内容: 1.QR分解定义 2.QR分解求法 3.QR分解与最小二乘 4.Matlab实现 一. ...
- Vsftpd服务传输文件(转)
本章节先通过介绍文件传输协议来帮助读者理解FTP协议的用处,安装vsftpd服务程序并逐条分析服务文件的配置参数. 完整演示vsftpd服务匿名访问模式.本地用户模式及虚拟用户模式的配置方法,介绍PA ...
- nginx 配置路由规则转发配置记录
工作中公司要求针对经销商PC端和工厂PC端的访问地址固定访问. 经销商PC端 http://localhost/ 工厂PC端 http://localhost/fac 文件磁盘路径: /crm/n ...
- js图片库 案例
事件处理函数:事件处理函数的作用是,在特定事件发生时调用特定的JavaScript代码.本例中想要在用户点击某个链接的时候触发一个动作,所以需要使用onclick事件处理函数. 添加事件处理函数的语法 ...
- 【转】javascript中not defined、undefined、null以及NaN的区别
原文链接(点击跳转) 第一:not defined 演示代码: <span style="font-size:12px;"><span style=" ...
- Objection, 一个轻量级的Objective-C依赖注入框架
简介 项目主页:https://github.com/atomicobject/objection 实例下载: https://github.com/ios122/ios122 Objection 是 ...
- Status bar - iOS之状态栏
(一)设置状态栏显示和隐藏 1.通过 Info.plist 文件增加字段,控制状态栏全局显示和隐藏 在 Info.plist 文件中增加字段 Status bar is initially hidde ...
- Centos7下MySql5.7安装及配置
安装MySql 软件包: mysql-community-libs-5.7.22-1.el7.x86_64.rpm mysql-community-common-5.7.22-1.el7.x86_64 ...
- dubbo 与Spring Cloud 对比
链接:https://www.zhihu.com/question/45413135/answer/242224410 近期也看到一些分享Spring Cloud的相关实施经验,这对于最近正在整理Sp ...