112.Path Sum (数的路径为N)
来源:互联网 发布:电器价格标签打印软件 编辑:程序博客网 时间:2024/06/14 12:24
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:Given the below binary tree and
sum = 22
,5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null){
return false;
}
if(root.left==null&&root.right==null){
return (root.val==sum) ;
}
return (hasPathSum(root.left, sum-root.val)||hasPathSum(root.right, sum-root.val));
}
}
------------------------------------------------------------------
void helper(TreeNode * root, int sum, int path[], int top) {
path[top++] = root.data;
sum -= root.data;
if (root->left == NULL && root->right==NULL) {
if (sum == 0) printPath(path, top);
} else {
if (root->left != NULL) helper(root->left, sum, path, top);
if (root->right!=NULL) helper(root->right, sum, path, top);
}
top --;
sum += root.data; //....
}
- 112.Path Sum (数的路径为N)
- Path Sum 二叉树中和为某一值的路径
- 寻找树根到叶子的权值之和为sum的路径 Path Sum
- [Leetcode]113. Path Sum II 求路径和一个数的所有路径
- leetcode系列(24)Path Sum 判断二叉树中和为Sum的路径是否存在
- 找出二叉树中和为k的所有路径 Path Sum II
- 113.Path Sum II&二叉树中和为某一值的路径
- Minimum Path Sum 最小值的路径
- LeetCode 112. Path Sum(路径和)
- Path Sum路径求和
- poj 1564 Sum It Up 【DFS】【求和为N的所有数的组合】
- 在n个整数中选k个,使选出来的数的和为sum
- 【二叉树】DFS计算路径和,保存路径【113. Path Sum II】【112. Path Sum】
- Leetcode-标签为Tree 112. Path Sum
- Path Sum 一个树里边,看看有没有一条路径上边的和等于给定的数
- 1-n之间的整数之和为指定数sum问题(百度之星往年试题)
- 无序数组中找出和为N的两个数 Two Sum
- 【DP】添加n个数+-,统计和为S的方案数【494. Target Sum】
- 架构、框架、模式、模块、组件、插件、控件、中间件的含义和区别
- Android:OTG+MediaScanner
- Mysql与Oracle区别
- Cell复用保留改变状态
- C++类访问控制--public,protected,private的使用(未完)
- 112.Path Sum (数的路径为N)
- redis配置文件详解配置文件redis.conf
- 升级Python版本后 yum命令不可用
- strcmp和strncmp
- iOS 字符属性NSAttributedString描述
- QT 中文转拼音
- UVA 10325
- HDU 2041 超级楼梯【递归】
- 操作分布式文件之六:如何解析远程文件