《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 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...
随机推荐
- Selenium入门18 断言
自动化测试需对比实际结果与预期结果,给出测试结论. 1 条件判断 if ...else... 2 assert ... #coding:utf-8 #断言 from selenium import w ...
- java运行顺序-JVM之九
简化顺序版本是: 父类的静态成员赋值和静态块,代码排版先后顺序执行,只运行一次 子类的静态成员赋值和静态块,代码排版先后顺序执行,只运行一次 父类代码块 父类构造方法 子类初始化块 子类构造方法 再次 ...
- 使用selenium 检测js报错
背景:接到一个需求,想检测页面是否能检测js报错,何为js报错,如下图所示,在控制台中,使用console,如果有js报错,就会出现错误 如何检测,简单版操作,打开一个url,使用manage获取浏览 ...
- hdu-2844&&POJ-1742 Coins---多重背包
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2844 题目大意: Tony想要买一个东西,他只有n中硬币每种硬币的面值为a[i]每种硬币的数量为c[ ...
- eclips新建Maven Web项目
一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven-archetype-webapp后,next 4.填写相应的信息,Packaged是默认创建 ...
- Netbackup:nbu常见错误及故障解决
Veritas Netbackup 提供了强大的故障响应功能, 能够有效及时的处理 各种备份故障.主要有备份状态码(status) .错误信息.报告信息及调试日志.下面我们主要针对备份状态码讲解下各种 ...
- bootstrap中模态框、模态框的属性
工作中有需要用到模态框的可以看看 <div class="modal fade" id="userModal" tabindex="-1&quo ...
- C# 方法中的参数类型
二.方法中的参数类型 1. 值参数 值参数是指不带修饰符只带数据类型的形参. 值参数在使用值向方法传递参数时,编译程序会把实参的值做一份拷贝,并且将此拷贝传递给该方法,被调用的方法不会修改内存中实参的 ...
- iRate快速绕坑使用
目的 iRate库通过激励用户去AppStore打分,来帮助你提升iPhone和Mac App的质量.这是取得经常使用的目标用户的意见的最好的方式之一. 方案(小弟想说的重点) 以前,App中都是显示 ...
- 牛客小白月赛2 G 文 【模拟】
链接:https://www.nowcoder.com/acm/contest/86/G来源:牛客网 题目描述 Sεlιнα(Selina) 开始了新一轮的男友海选.她要求她的男友要德智体美劳样样都全 ...