Leetcode 437. Path Sum III
来源:互联网 发布:seo软件下载 编辑:程序博客网 时间:2024/05/17 01:25
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.
简单的归递。public class Solution { public int pathSum(TreeNode root, int sum) { if (root == null) return 0; int res = 0; res += dfs(root, sum); res += pathSum(root.left, sum); res += pathSum(root.right, sum); return res; } private int dfs(TreeNode node, int remain) { int tmp = 0; if (node.left != null) tmp += dfs(node.left, remain-node.val); if (node.right != null) tmp += dfs(node.right, remain-node.val); if (remain == node.val) tmp++; return tmp; }}
0 0
- LeetCode 437. Path Sum III
- LeetCode 437. Path Sum III
- LeetCode 437. Path Sum III
- leetcode 437. Path Sum III
- [LeetCode]437. Path Sum III
- 【LeetCode】 437. Path Sum III
- [leetcode]437. Path Sum III
- LeetCode 437. Path Sum III
- [leetcode]437. Path Sum III
- [LeetCode]437. Path Sum III
- Leetcode-437. Path Sum III
- Leetcode-437. Path Sum III
- LeetCode-437. Path Sum III
- 【Leetcode】437. Path Sum III
- 【Leetcode】437. Path Sum III
- Leetcode 437. Path Sum III
- [LeetCode]437. Path Sum III
- [leetcode]: 437. Path Sum III
- 257. Binary Tree Paths
- ScrollView和listView嵌套使用
- TOMCAT JAVA_HOME or JRE_HOME environment variable is not defined correctly
- 理解RESTful架构
- C++ 二叉树创建、遍历访问、删除
- Leetcode 437. Path Sum III
- caffe训练提示top blob 'data' produced multiple sour解决办法
- FZU1062 NBUT1225
- Tomcat 优化方案 和 配置详解
- (四)频繁插入和删除时使用LinkedList
- 自定义View的知识点
- Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释
- #对象数组#集合
- 分类和逻辑回归(Classification and logistic regression)