437. Path Sum III
2017-09-03 18:05
323 查看
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.
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); }
相关文章推荐
- 437. Path Sum III
- [LeetCode]437. Path Sum III
- 437. Path Sum III
- 【LeetCode】112,113,437. Path Sum I, II, III
- leetcode_437. Path Sum III-二叉树
- Leetcode 437. Path Sum III (Easy) (cpp)
- LeetCode笔记:437. Path Sum III
- leetcode 437. Path Sum III 深度优先遍历DFS+两个递归函数
- leetcode 437. Path Sum III(路径和)(DFS)
- 437. Path Sum III
- 【LeetCode】 437. Path Sum III
- LeetCode - 437. Path sum III
- LeetCode 437. Path Sum III (STL map前缀和)
- Leetcode 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- [LeetCode]437. Path Sum III
- 437. Path Sum III(二叉树的路径和之三)
- [leetcode]437. Path Sum III
- LeetCode-437. Path Sum III