《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 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...
随机推荐
- 古老的pike
快速略读了一下源码,记了一些东西. 先看看mapping mapping其实就是C++中的multimap,但是支持更多. array values(mapping).这个方法可以返回所有mappin ...
- 西门子 SINAMICS S120 Web server 用户名和默认密码
sinamics web server可以通过浏览器查看驱动器故障等信息,是一个比较方便的辅助工具. 1. 一般用户 SINAMICS 密码 无 2. 管理员 Administrator 密码 Adm ...
- IntelliJ IDEA环境使用
转:https://blog.csdn.net/zwj1030711290/article/details/80673482 https://blog.csdn.net/zrc199021/artic ...
- 启动Windows服务
实现效果: 知识运用: ServiceController类的ServiceName Status属性 public string ServiceName {get; set;} //对此Servic ...
- json 序列化和反序列化的3个方法
https://www.cnblogs.com/caofangsheng/p/5687994.html
- svn update 时总是提示 Password for '默认密钥' GNOME keyring: 输入密码
执行svn update 时 总是提示输入密码. $ cd ~/.gnome2/keyrings/ $ ls $ rm 默认密钥.keyring
- python 添加 threadpool
操作系统: Ubuntu 10.04 python安装依赖的软件包: python 出现 ImportError: No module named ** 我这里出现了: ImportError: No ...
- 编写第一个OpenACC程序
原文链接 在PGI的官方网站上获得示例代码: http://www.pgroup.com/lit/samples/pgi_accelerator_examples.tar 我们的第一个例子从一个简单的 ...
- Mathematica 讲座
Mathematica 讲座笔记本 [下载] 第一章 Mathematica 简介 [观看] [下载] 第二章 Mathematica 界面和编程语言 [观看] [下载] 第三章 符号运算 [观看] ...
- 配置伪静态(URL重写)
本篇借鉴了很多文章,这里做个记录. 有时我们的导航栏出现xx.aspx?id=x&name=xx 等等这样,会显得不好看,我们可以利用伪静态来美化我们的导航栏,伪静态的形式可以自己定义,本质还 ...