《Cracking the Coding Interview》——第4章:树和图——题目9
2014-03-19 05:07
题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径。路径的起点和终点都可以是树的任意节点。
解法:我偷了个懒,直接把这棵树看成一个无向图,用DFS来进行暴力搜索解决问题。因为没有什么数据顺序或是范围的限制,所以搜索剪枝好像也不太容易。
代码:
// 4.9 Find all paths in a binary tree, the path doesn't have to start or end at the root or a leaf node.
#include <cstdio>
#include <unordered_map>
#include <unordered_set>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right; TreeNode(int _val = ):val(_val), left(nullptr), right(nullptr) {};
}; void constructTree(TreeNode *&root)
{
int val; scanf("%d", &val);
if (val == ) {
root = nullptr;
} else {
root = new TreeNode(val);
constructTree(root->left);
constructTree(root->right);
}
} void constructGraph(TreeNode *root, unordered_map<TreeNode *, unordered_set<TreeNode *> > &graph)
{
if (root->left != nullptr) {
graph[root].insert(root->left);
graph[root->left].insert(root);
constructGraph(root->left, graph);
} if (root->right != nullptr) {
graph[root].insert(root->right);
graph[root->right].insert(root);
constructGraph(root->right, graph);
}
} void DFS(TreeNode *node, vector<TreeNode *> &path, int sum, const int target,
unordered_set<TreeNode *> &checked,
unordered_map<TreeNode *, unordered_set<TreeNode *> > &graph, vector<vector<TreeNode *> > &result)
{
path.push_back(node);
checked.insert(node); if (sum == target) {
result.push_back(path);
}
unordered_set<TreeNode *>::iterator it;
for (it = graph[node].begin(); it != graph[node].end(); ++it) {
if (checked.find(*it) == checked.end()) {
DFS(*it, path, sum + (*it)->val, target, checked, graph, result);
}
} checked.erase(node);
path.pop_back();
} void doDFS(TreeNode *root, vector<TreeNode *> &path, const int target,
unordered_set<TreeNode *> &checked,
unordered_map<TreeNode *, unordered_set<TreeNode *> > &graph, vector<vector<TreeNode *> > &result)
{
path.clear();
checked.clear();
DFS(root, path, root->val, target, checked, graph, result);
if (root->left != nullptr) {
doDFS(root->left, path, target, checked, graph, result);
}
if (root->right != nullptr) {
doDFS(root->right, path, target, checked, graph, result);
}
} void clearTree(TreeNode *&root)
{
if (root == nullptr) {
return;
} if (root->left != nullptr) {
clearTree(root->left);
}
if (root->right != nullptr) {
clearTree(root->right);
}
delete root;
root = nullptr;
} int main()
{
int i, j;
int target;
TreeNode *root;
unordered_map<TreeNode *, unordered_set<TreeNode *> > graph;
unordered_map<TreeNode *, unordered_set<TreeNode *> >::iterator it;
unordered_set<TreeNode *> checked;
vector<TreeNode *> path;
vector<vector<TreeNode *> > result; while (true) {
constructTree(root);
if (root == nullptr) {
break;
}
constructGraph(root, graph);
while (scanf("%d", &target) == && target) {
doDFS(root, path, target, checked, graph, result);
if (result.empty()) {
printf("No path is found.\n");
} else {
for (i = ; i < (int)result.size(); ++i) {
printf("%d", result[i][]->val);
for (j = ; j < (int)result[i].size(); ++j) {
printf("->%d", result[i][j]->val);
}
result[i].clear();
printf("\n");
}
result.clear();
}
path.clear();
checked.clear();
}
for (it = graph.begin(); it != graph.end(); ++it) {
(it->second).clear();
}
graph.clear();
clearTree(root);
} return ;
}
《Cracking the Coding Interview》——第4章:树和图——题目9的更多相关文章
- 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(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 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(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(二)
3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...
- 《Cracking the Coding Interview》——第4章:树和图——题目8
2014-03-19 05:04 题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树.子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样. 解法:首先可以根据 ...
随机推荐
- April 19 2017 Week 16 Wednesday
What would life be if we had no courage to attempt anything? 如果我们都没有勇气去尝试点什么,生活会变成什么样子呢? I remembere ...
- SAP成都研究院Sunshine: 我的C4C实习感受和保研之路
今天的文章来自SAP成都一位实习生,曾经和Jerry同在C4C成都开发团队一起工作过.在Sunshine最后一个工作日里,Jerry和Sunshine一起吃饭的时候,她曾经聊到接下来的保研打算和将来工 ...
- NutDao配置多数据源
首先,我必须声明,这是一个非常简单的方法,很多小菜没做出来,是因为把nutz想得太复杂 数据源(或者是数据库连接池),在Nutz.Ioc看来,是一个普通的Bean,没任何特别之处. 再强调一点,除了$ ...
- BZOJ 4247 挂饰 01背包
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4247 JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机 ...
- left join后面加上where条件浅析
select a.*,b.* from table1 a left join table2 b on b.X=a.X where XXX 如上:一旦使用了left join,没有where条件时,左表 ...
- js字符串内容包含单引号‘’和双引号“”怎么办?
如果javascript中的字符串包含单引号和双引号,可以用转义字符来标识 'I\'m \"OK\"!'; 表示的字符串内容是:I'm "OK"! 转义字符\可 ...
- Subversion简介
作为一名编程人员,SVN经常作为代码.项目的版本控制,殊不知SVN也可作为其他领域的版本控制,例如对文档.音频.视频等 . SVN可以看成一种文件系统,为了使工作人员提高工作效率,可以进行并行的工作, ...
- 网际协议 IP
网际协议 网际协议(internet protocol),简称IP; 概念:TCP/IP网络体系结构中网际层的协议.用以提供无连接的数据服务. 1.IP地址的概念及组成 概念:IP地址就是用来唯一标 ...
- 在ubuntu中docker的简单使用(一)
>>docker version 当运行docker version 命令出现Cannot connect to Docker daemon. Is the docker daemon r ...
- 没有CTO的Netflix有哪些值得我们学习的工程文化?
作者介绍: 杨波,拍拍贷基础框架研发总监.具有超过 10 年的互联网分布式系统研发和架构经验,曾先后就职于:eBay 中国研发中心(eBay CDC),任资深研发工程师,参与亿贝开放 API 平台研发 ...