剑指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 ...
随机推荐
- Cable master--hdu1551(二分法)
Cable master Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- android 测量控件视图的方法
在实际项目中经常要用到 测量一个控件或者视图的高,宽.然后根据这个高宽进行一些逻辑. 计算视图宽高有几种方式先简单的了解下android 视图的绘制过程会促进理解. 一.android View绘制过 ...
- Oracle EBS-SQL (PO-18):检查工作台下达的PR在系统找不到.sql
select * From apps.po_requisitions_interface_all---------------------------------------------------- ...
- Oracle EBS-SQL (CST-1):检查BOM历史成本查询(Average Cost).sql
select msi1.segment1 父件编码, msi1.description 父件描述, msi1.primary_u ...
- android的注意点
1.使用Message.callback Message msg = Message.obtain(myThreadHandler,new Runnable() { @Override public ...
- 解决GitHub未配置SSH key提示错误信息
git push -u origin master Permission denied (publickey). fatal: Could not read from remote repositor ...
- 751D·PARK北京时尚设计广场_百度百科
751D·PARK北京时尚设计广场_百度百科 751D·PARK北京时尚设计广场
- JAVA File类 分析(三)
前面两篇与大家一起研究了unix下的文件系统,本篇将和大家一起分析 文件的属性和文件夹. ok,废话不说,先来段代码 #include <stdio.h> #include <sys ...
- .NET MV4 Remote远程验证注意事项及案例
首先是模型代码 public class LoginModel { [Required] [Display(Name = "用户名")] [Remote("CheckNa ...
- 解決 IE10 浏览器无法使用 ASP.NET From 验证登录的问题
最近应项目用到ASP.Net表单验证机制(FormsAuthentication),来判断用户是否已经登录,一切测试顺利,最后发布到IIS中后在IE10测试是发现始终判断用户没登录(其他浏览器一切正常 ...