437. Path Sum III
来源:互联网 发布:网络在线平交流平台 编辑:程序博客网 时间:2024/06/16 00:32
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
- 列表内容/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int pathSumRoot(struct TreeNode* root,int sum) { int count = 0; if(root == NULL){ return count; } if(root->val == sum){ count++; } /*left node*/ if(root->left){ count += pathSumRoot(root->left,sum-root->val); } /*right node*/ if(root->right){ count += pathSumRoot(root->right,sum-root->val); } return count;}int pathSum(struct TreeNode* root, int sum) { int count = 0; if(root == NULL){ return 0; } if(root->left == NULL && root->right == NULL){ if(root->val == sum){ return 1; }else{ return 0; } } return pathSumRoot(root,sum) + pathSum(root->left,sum) + pathSum(root->right,sum);}
阅读全文
0 0
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III*
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- HotSpot C2编译器
- linux 下面SCP拷贝文件
- BZOJ 3932 CQOI2015 主席树
- BZOJTree Rotations(线段树的合并)
- WIN7 64 位旗舰版安装 CUDA 6.0 过程(显卡为NVIDIA GT 440)
- 437. Path Sum III
- mysql深入二
- 关于监控系统的一些想法心得
- java泛型使用
- java面试-判断+代码查错
- 006-SLF4J的简单使用
- UVA 839
- 通过金矿模型介绍动态规划
- 微信小程序开发过程中遇到的问题