JD 1368:二叉树中和为某一值的路径
来源:互联网 发布:淘宝网电器商城 编辑:程序博客网 时间:2024/05/16 07:06
OJ题目:click here~~
题目分析:找二叉树中和为某一值的路径。。。
剑指offer 面试题25
AC_CODE
const int maxn = 10008 ;int n , k ;struct Node{ int x ; int l , r ; Node():l(-1),r(-1){}}tree[maxn];void FindPath(int t , int sum , vector<int> &path){ if(tree[t].l == -1 && tree[t].r == -1 ){ if(sum + tree[t].x == k){ path.push_back(t) ; printf("A path is found:") ; for(int i = 0;i < path.size();i++) printf(" %d" , path[i]) ; puts("") ; path.pop_back() ; } return ; } else{ if(sum + tree[t].x >= k) return ; else{ path.push_back(t) ; FindPath(tree[t].l , sum + tree[t].x , path) ; FindPath(tree[t].r , sum + tree[t].x , path) ; path.pop_back() ; } }}int main(){ while(cin >> n >> k){ int i , w , l , r ; for(i = 1;i <= n;i++){ scanf("%d%d%d" , &w , &l , &r) ; tree[i].x = w ; tree[i].l = min(l , r) ; tree[i].r = max(l , r) ; } vector<int> path ; puts("result:") ; FindPath(1 , 0 , path) ; } return 0 ;}
0 0
- JD 1368:二叉树中和为某一值的路径
- 题目1368:二叉树中和为某一值的路径
- 1368:二叉树中和为某一值的路径 @jobdu
- 题目1368:二叉树中和为某一值的路径
- 题目1368:二叉树中和为某一值的路径
- 题目1368:二叉树中和为某一值的路径
- 题目1368:二叉树中和为某一值的路径
- 【树】二叉树中和为某一值的路径
- 【树6】二叉树中和为某一值的路径
- 题目11:二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的所有路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 我的博客开通了
- 我的博客开通了
- 工作笔记
- 1、博客之家
- Fpga的vga显示设计(一)
- JD 1368:二叉树中和为某一值的路径
- hdu1047 Integer Inquiry 多次大数相加
- android学习笔记5-JDBC编程
- 我的博客开通了
- c语言文件操作相关函数
- 我的博客开通了
- 设计模式-工厂演化
- 我的博客开通了
- 我的博客开通了