剑指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 ...
随机推荐
- 【转】Delphi内嵌ASM简易教程
Delphi内嵌ASM简易教程 作者:heiying2006-03-19 18:33分类:默认分类标签: 前言 Delphi作为一个快速高效的开发平台,使用的人越来越多,但熟悉在Delphi代码中嵌入 ...
- Oracle监控指标
1.数据文件或数据设备 参考:http://f.dataguru.cn/thread-106901-1-1.html2.数据库日志空间活或回滚段(包括大小.设备.文件及可用率.日志空间竞争情况或回滚段 ...
- Tip.It诞生记
灵光一闪 之所以想做 Tip.It,完全是受自己初到北京时找房子各种痛苦的启发,当时为了跳过中介租房子,去58同城,豆瓣等各种网站去看房子,比如说下图就是一个典型的58同城的租房页面. 可以看到电话号 ...
- (续)线性表之双向链表(C语言实现)
在前文实现单向链表的基本操作下,本文实现双向链表的基本操作. 双向链表与单链表差异,是双向链表结点中有前向指针和后向指针.所以在插入和删除新结点元素时候不见要考虑后向指针还要考虑前向指针. 以下是双向 ...
- POJ 1724 最短路费用限制
迪杰斯塔拉裸题 最大花费 n个点 m条有向边 起点终点 路径长度 路径花费 问:在花费限制下,最短路径的长度 #include <iostream> #include <string ...
- C语言课程设计—图书管理系统
这是本人大一第二学期初C语言课程设计的作品,嘿嘿,本来以为已经找不到原稿了,今天无意中居然在QQ网络硬盘中找到了当初的teta版,公布于此,以作纪念. C源码例如以下: #include<std ...
- java整合easyui进行的增删改操作
首先发一下效果图 显示全部用户信息 加入用户信息 删除用户信息 编辑用户信息 以下就来介绍一下easyui的crud,在java中是怎么与后台进行交换的 前台html页面,我将它命名为crud1.ht ...
- 【精度问题】【HDU2899】Strange fuction
Strange fuction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Linux学习4——Vim和Bash
一.写在前面 本将将介绍Linux自带的强大的文本编辑器Vim和Bash的相关知识. 二.完成目标 1.了解Vim和Bash的基本概念 2.定制自己的vim 3.Bash中的一些命令 4.管道命令 ...
- 解决水晶报表在IIS7下的权限问题。
http://52live.blog.sohu.com/69025059.html 解决水晶报表在IIS7下的权限问题. 有些事情真是“踏破铁鞋无觅处,得来全不费功夫”!困扰了我一段时间的水晶报表在I ...