Cracking the Coding Interview 4.8
You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree-it does not have to start at the root.
思路:
既然路径的开始不一定在根,那么找出从根开始的所有路径就不够了。每个节点都有可能是开始,也有可能是结束。将任一节点当做结束节点,倒推至根节点,路径上只要有和为value,就是一个满足的路径,但是找到一个还不能停,一直要倒推到根为止,因为可能倒推序列是2+3-1+1,如果value是5,那么2+3满足,但2+3-1+1也满足,因此我们一直要倒推到根,才能找出所有的路径。
void func(Node *n,int *buf,int level,int sum)//n是结束节点,buf里存放了从该节点到根每个节点上的key,level是深度,sum是要求的和
{
if(n == NULL)
{
return;
}
buf[level]=n->key;
int temp = sum;
for(int i=level;i>=;i--)
{
temp-=buf[i];
if(temp==)
{
for(int j=i;j<level;j++)
{
printf("%d,",buf[j]);
}
printf("%d\n",buf[level]);
}
}
func(n->left,buf,level+,sum);
func(n->right,buf,level+,sum);
}
Cracking the Coding Interview 4.8的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 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》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- 【转载】resolv.conf中search作用
原文地址:http://www.oliver.ren/linux/387.html reslov.conf中的search主要是用来补全hostname的,有时候域名太长,可以做一个短域名做主机名字, ...
- ImageMagick的下载和配置
2. 新建一个VC++的工程,项目->属性 VC++目录中 包含目录中加入4项,ImageMagick安装路径下的include文件夹,和include里边的三个文件夹. 库目录中加入1项,Im ...
- API开发管理平台eoLinker AMS 4.1版本发布:加入聚合空间,发布AMS专业版等
eoLinker AMS是集API文档管理.API自动化测试.开发协作三位一体的综合API开发管理平台,是中国最大的在线API管理平台. eoLinker AMS 4.1更新内容: 1.新增" ...
- Ubuntu网卡设置:配置/etc/netplan
对于Ubuntu1804版本,经过测试如下配置可以设置静态IP地址: Google@ubuntu:~$ cat /etc/netplan/01-netcfg.yaml network: etherne ...
- array_map 等php回调函数使用问题(关联数组下标获取)
前言:我自己用此类回调函数,来替代 foreach 纯粹是用为代码的简洁性,让代码更好看.(我有点代码小洁癖~) 1.array_reduce 当迭代处理一个一维索引数组时,在回调函数内是无法获取到当 ...
- Flask - 路由系统
目录 Flask - 路由系统 @app.route()装饰器中的常用参数 methods : 当前 url 地址,允许访问的请求方式 endpoint:反向url地址,默认为视图函数名(url_fo ...
- BFOA
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> ...
- bug的分类和等级
一.bug的定义 软件的bug,狭义指软件程序的漏洞或缺陷,广义指测试工程师或用户提出的软件可改进的细节.或与需求文档存在差异的功能实现等 对应三个测试目的:(3个为了) 1.为了发现程序的代码或业务 ...
- everyday two problems / 3.1
T1.string 题意: 给定由小写字母组成的长度为 n 的字符串 将其所有 n * (n + 1) / 2 个子串按字典序排序 输出第 k 个子串 k > (n * (n + 1) / 2) ...
- CodeForcesGym 100735D Triangle Formation
Triangle Formation Time Limit: Unknown ms Memory Limit: 65536KB This problem will be judged on CodeF ...