剑指Offer:面试题25
题目描述:
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
九度OJ地址:http://ac.jobdu.com/problem.php?pid=1368 自己写的代码,自己运行没问题,提交总是不通过 = =
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<memory.h>
using namespace std; #define MAX 100
struct BinaryTreeNode{
int index;
int value;
int lchild;
int rchild;
}; BinaryTreeNode p[MAX];
vector<BinaryTreeNode> path; void FindPath(BinaryTreeNode* p,int exceptedNum,vector<BinaryTreeNode> path,int currentNum){
currentNum=currentNum+(p->value);
path.push_back(*p);
bool IsLeaf=(p->lchild==-)&&(p->rchild==-); if(currentNum==exceptedNum&&IsLeaf)
{
printf("A path is found: ");
vector<BinaryTreeNode>::iterator iter=path.begin();
for(;iter!=path.end();++iter)
{
printf("%d ",iter->index);
}
printf("\n");
}
if(p->lchild!=-)
FindPath(p+(p->lchild-p->index),exceptedNum,path,currentNum);
if(p->rchild!=-)
FindPath(p+(p->rchild-p->index),exceptedNum,path,currentNum);
currentNum=currentNum-(p->value);
path.pop_back(); }
void FindPath(BinaryTreeNode* p,int exceptedNum){
if(p==NULL)
return;
int currentNum=;
FindPath(p,exceptedNum,path,currentNum); }
int main()
{
int n,k;
int v,l,r;
while(scanf("%d %d",&n,&k)==)
{
memset(p,-,sizeof(p));
for(int i=;i<n;i++)
{
scanf("%d %d %d",&v,&l,&r);
p[i].index=i+;
p[i].value=v;
p[i].lchild=min(l,r);
p[i].rchild=max(l,r);
}
printf("result:\n");
FindPath(p,k); }
return ;
}
找了大神代码完美运行:
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<memory.h>
using namespace std; #define MAX 100
struct BinaryTreeNode{
int index;
int value;
int lchild;
int rchild;
}; BinaryTreeNode p[MAX];
vector<BinaryTreeNode> path; void FindPath(BinaryTreeNode* p,int exceptedNum,vector<BinaryTreeNode> path,int currentNum){
currentNum=currentNum+(p->value);
path.push_back(*p);
bool IsLeaf=(p->lchild==-)&&(p->rchild==-); if(currentNum==exceptedNum&&IsLeaf)
{
printf("A path is found: ");
vector<BinaryTreeNode>::iterator iter=path.begin();
for(;iter!=path.end();++iter)
{
printf("%d ",iter->index);
}
printf("\n");
}
if(p->lchild!=-)
FindPath(p+(p->lchild-p->index),exceptedNum,path,currentNum);
if(p->rchild!=-)
FindPath(p+(p->rchild-p->index),exceptedNum,path,currentNum);
currentNum=currentNum-(p->value);
path.pop_back(); }
void FindPath(BinaryTreeNode* p,int exceptedNum){
if(p==NULL)
return;
int currentNum=;
FindPath(p,exceptedNum,path,currentNum); }
int main()
{
int n,k;
int v,l,r;
while(scanf("%d %d",&n,&k)==)
{
memset(p,-,sizeof(p));
for(int i=;i<n;i++)
{
scanf("%d %d %d",&v,&l,&r);
p[i].index=i+;
p[i].value=v;
p[i].lchild=min(l,r);
p[i].rchild=max(l,r);
}
printf("result:\n");
FindPath(p,k); }
return ;
}
慢慢学习吧
剑指Offer:面试题25的更多相关文章
- 剑指offer——面试题25:合并两个 排序的链表
自己答案: ListNode* MergeTwoSortedList(ListNode* pHead1,ListNode* pHead2) { if(pHead1==nullptr&& ...
- 剑指Offer:面试题25——二叉树中和为某一值的路径(java实现)
问题描述: 输入一棵二叉树和一个整数,打印出二叉树中结点指的和为输入整数的所有路径.从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.二叉树结点的定义如下: public class Tree ...
- 剑指offer面试题25:二叉树中和为某一值的路径
题目:输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.从根节点开始往下一直到叶节点所经过的节点形成一条路径. 解题思路:当使用前序遍历的方式访问某一节点时,把该节点添加到路径上 ...
- 剑指Offer:面试题15——链表中倒数第k个结点(java实现)
问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...
- 剑指offer面试题3 二维数组中的查找(c)
剑指offer面试题三:
- 剑指Offer——笔试题+知识点总结
剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解
剑指offer 面试题23:从上往下打印二叉树 参与人数:4853 时间限制:1秒 空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...
随机推荐
- Spring中的DataBinding(一)
DataBinding在Spring中应用. 第一点:使用ModelAttribute指定带绑定的数据内容 很重要的属性:@ModelAttribute([value=""])可以 ...
- Linux下编译安装qemu和libvirt
目录 [hide] 1 安装qemu 1.1 qemu介绍 1.2 下载源文件 1.3 编译安装 2 安装libvirt 2.1 libvirt介绍 2.2 下载libvirt 2.3 编译安装 3 ...
- IPicture总结
1.利用IPicture接口加载.显示图片 IPicture接口管理一个图片对象和它的属性.图片对象提供对Bitmap Icon Metafile的语言不相关的抽象支持.图像对象的主要接口是IPict ...
- 【HDU 5572 An Easy Physics Problem】计算几何基础
2015上海区域赛现场赛第5题. 题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5572 题意:在平面上,已知圆(O, R),点B.A(均在圆外),向量 ...
- convert用法(数据库中原本储存的格式是Nvarchar,如何修改成datetime格式)
查询这张表得到的数据如图 select CONVERT(nvarchar,substring([purchase-date],1,4)) +'-'+CONVERT(nvarchar,substring ...
- nyoj 1238 最少换乘(dijkstra)
描述 欧洲某城是一个著名的旅游胜地,每年都有成千上万的人前来观光旅行.Dr. Kong决定利用暑假好好游览一番.. 年轻人旅游不怕辛苦,不怕劳累,只要费用低就行.但Dr. Kong年过半百,他希望乘坐 ...
- ASP.NETURL地址防注入过滤问题
首先在Global.asax.cs里面配置一个 提交事件 不用过滤所有的地址 过滤 GET POST的地址就行了 /// <summary> /// 防止sql注入 /// </s ...
- K-th Number(第k大数)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 45710 Accepted: 15199 Ca ...
- ListView嵌套ListView时发生:View too large to fit into drawing cache的问题
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXkxMzg3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- 图的邻接表存储 c实现
图的邻接表存储 c实现 (转载) 用到的数据结构是 一个是顶点表,包括顶点和指向下一个邻接点的指针 一个是边表, 数据结构跟顶点不同,存储的是顶点的序号,和指向下一个的指针 刚开始的时候把顶点表初始化 ...