Path Sum
2016-05-24 09:45
330 查看
【题目】
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
return true, as there exist a root-to-leaf path
【题意】
判断二叉树是否存在路径和等于sum的路径,若存在输出true,否则输出false
【分析】
递归调用二叉树,每次将上一层的val值传递给子结点并加上子节点的val,当传递到某个结点为叶子结点时,判断其val值是否等于sum
【实现】
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
return pathFind(root, sum);
}
public boolean pathFind(TreeNode root, int sum){
if (root == null){
return false;
}
if (root.left == null && root.right == null){
if( sum - root.val == 0){
return true;
}
}
return (pathFind(root.left, sum - root.val) || pathFind(root.right, sum - root.val));
}
}c++
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool dfs(TreeNode *node, int sum, int curSum)
{
if (node == NULL)
return false;
if (node->left == NULL && node->right == NULL)
return curSum + node->val == sum;
return dfs(node->left, sum, curSum + node->val) || dfs(node->right, sum, curSum + node->val);
}
bool hasPathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return dfs(root, sum, 0);
}
};
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->2which sum is 22.
【题意】
判断二叉树是否存在路径和等于sum的路径,若存在输出true,否则输出false
【分析】
递归调用二叉树,每次将上一层的val值传递给子结点并加上子节点的val,当传递到某个结点为叶子结点时,判断其val值是否等于sum
【实现】
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
return pathFind(root, sum);
}
public boolean pathFind(TreeNode root, int sum){
if (root == null){
return false;
}
if (root.left == null && root.right == null){
if( sum - root.val == 0){
return true;
}
}
return (pathFind(root.left, sum - root.val) || pathFind(root.right, sum - root.val));
}
}c++
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool dfs(TreeNode *node, int sum, int curSum)
{
if (node == NULL)
return false;
if (node->left == NULL && node->right == NULL)
return curSum + node->val == sum;
return dfs(node->left, sum, curSum + node->val) || dfs(node->right, sum, curSum + node->val);
}
bool hasPathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return dfs(root, sum, 0);
}
};
相关文章推荐
- 关于slax
- 小知识
- 知道这20个正则表达式,能让你少写1,000行代码
- mq集群
- linux修改系统时间date命令加clock -w
- 记第一次主持
- jdbc select
- GitHub Top 100的Android开源库
- 检测使用 three.js 时的 WebGL 和浏览器兼容性(Detecting WebGL and browser compatibility with three.js)(转)
- ASP.NET使用xslt将xml转换成Excel
- java 关于SimpleDateFormat线程安全问题
- MySQL 索引的创建、删除和查看操作
- SharedPreferences存储用户名
- 第二次冲刺阶段第一天
- 堆栈信息打印
- Adobe AIR 代码签名证书使用指南
- java实现无组件导出CSV文件
- 苹果Mac系统开发必备资源
- 【C语言】数组与指针
- 使用LayoutParams设置布局