题目描述:
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
九度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的更多相关文章

  1. 剑指offer——面试题25:合并两个 排序的链表

    自己答案: ListNode* MergeTwoSortedList(ListNode* pHead1,ListNode* pHead2) { if(pHead1==nullptr&& ...

  2. 剑指Offer:面试题25——二叉树中和为某一值的路径(java实现)

    问题描述: 输入一棵二叉树和一个整数,打印出二叉树中结点指的和为输入整数的所有路径.从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.二叉树结点的定义如下: public class Tree ...

  3. 剑指offer面试题25:二叉树中和为某一值的路径

    题目:输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.从根节点开始往下一直到叶节点所经过的节点形成一条路径. 解题思路:当使用前序遍历的方式访问某一节点时,把该节点添加到路径上 ...

  4. 剑指Offer:面试题15——链表中倒数第k个结点(java实现)

    问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...

  5. 剑指offer面试题3 二维数组中的查找(c)

    剑指offer面试题三:

  6. 剑指Offer——笔试题+知识点总结

    剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...

  7. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  8. C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解

    剑指offer  面试题23:从上往下打印二叉树 参与人数:4853  时间限制:1秒  空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...

  9. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  10. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

随机推荐

  1. git 拉取远程分之到本地

    git checkout -b newbranch_name --track origin/feature/newbranch_name 如果遇到类似: fatal: git checkout: up ...

  2. OpenFileDialog 害人的RestoreDirectory

    莫名其妙出现找不到文件的错误.经查,发现: OpenFileDialog,SaveFileDialog在选择文件后,会切换当前程序目录的路径(System.Environment.CurrentDir ...

  3. 国内5款优秀的WEB前端框架

    1. JX(腾讯) 官网地址:http://alloyteam.github.io/JX/#home JX 是一个类似 Google Closure Library 的 Web 前端开发框架,服务于 ...

  4. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  5. hdu 2665 Kth number_划分树

    题意:求区间[a,b]的第k大 因为多次询问要用到划分树 #include <iostream> #include<cstdio> #include<algorithm& ...

  6. 【LeetCode练习题】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  7. #include <boost/scoped_array.hpp>

    多个元素使用#include <boost/scoped_array.hpp> 单个元素使用#include <boost/scoped_ptr.hpp> 作用域数组 作用域数 ...

  8. hdu 2660 Accepted Necklace(dfs)

    Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...

  9. LSH算法原理

    原始链接--http://www.jiahenglu.net/NSFC/LSH.html LSH(Location Sensitive Hash),即位置敏感哈希函数.与一般哈希函数不同的是位置敏感性 ...

  10. Java Swing界面编程(27)---JRadioButton事件处理

    在单选button操作中.能够使用ItemListener接口进行事件的监听. package com.beyole.util; import java.awt.Container; import j ...