题目:

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.

代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if (!root) return false;
int next = sum - root->val;
if ( !root->left && !root->right ) return sum==root->val;
if ( root->left && !root->right ) return Solution::hasPathSum(root->left, next);
if ( !root->left && root->right ) return Solution::hasPathSum(root->right, next);
return Solution::hasPathSum(root->left, next) || Solution::hasPathSum(root->right, next);
}
};

tips:

一开始没理解好题意。

这个题要求必须走到某条path的叶子节点才算数,因此终止条件为走到叶子节点或者NULL。此外,root->left或者root->right不为NULL才往这个分支走。

============================================

第二次过这道题,终止条件是要走到叶子节点,但是参数传递写的有点儿啰嗦。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum)
{
bool find = false;
if(root) Solution::pathSum(root, sum, , find);
return find;
}
static void pathSum(TreeNode* root, int sum, int pathsum, bool& find)
{
if ( find ) return;
if ( !root->left && !root->right )
{
if ( (pathsum+root->val)==sum )
{
find = true;
return;
}
}
if ( root->left ) Solution::pathSum(root->left, sum, pathsum+root->val, find);
if ( root->right ) Solution::pathSum(root->right, sum, pathsum+root->val, find);
}
};

【Path Sum】cpp的更多相关文章

  1. 【Binary Tree Maximum Path Sum】cpp

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  2. 【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...

  3. 【Minimum Path Sum】cpp

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  4. 【Combination Sum 】cpp

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  5. 【Two Sum】cpp

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  6. 【Path Sum II】cpp

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  7. 【二叉树的递归】05二叉树中找任意起点和终点使他们的路径和最大【Binary Tree Maximum Path Sum】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,寻找值最大的路径. ...

  8. 【二叉树的递归】04找出二叉树中路径和等于给定值的所有路径【Path Sum II】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...

  9. leetcode 【 Minimum Path Sum 】python 实现

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

随机推荐

  1. 创建带sshd服务的docker image

    参考:https://docs.docker.com/examples/running_ssh_service/ 1. 创建一个空目录用于存放Dockerfile mkdir -p /home/thm ...

  2. Win2003打不开https的问题

    碰到客户做问题是能打开https://www.baidu.com 这个网页 打不开 https://sha256.alipay.com/SHA256/index.htm支付宝这个网页      解决办 ...

  3. Dockpanel的控件加载问题

    1. 正确加载模式:panel.ControlContainer.Controls.Add(control); 如果用panel.Controls.Add(control);则可能出现模块发生位移问题 ...

  4. sqoop的codegen工具

    一.codegen工具的使用 sqoop codegen --connect jdbc:mysql://localhost:3306/test --username root --password 1 ...

  5. scala学习笔记2

    一.算术和操作符重载 a + b 是如下方法的简写: a.+(b) 在scala中你可以使用任何符号来为方法命名.比如BigInt类就定义了一个/%的方法,该方法返回一个对偶,对偶的内容是除法操作得到 ...

  6. oracle函数和存储过程示例

    Function: --为了使产生的uuid符合rfc 4122的标准(http://tools.ietf.org/html/rfc4122),例如:a8f662b8-6e7a-13fe-e040-9 ...

  7. DTD 知识归纳总结

    一:在xml文件中引用一个dtd规则. <!DOCTYPE 根元素 [元素声明]> 二: xml文档中中包含的内容模块 元素:元素是 XML 以及 HTML 文档的主要构建模块. 属性:属 ...

  8. php循环创建目录

    代码取自thinkphp中: function mk_dir($dir, $mod = 0777) { if(!is_dir($dir) || mkdir($dir, $mod)) { if(!mk_ ...

  9. PHP+MYSQL会员系统的开发实例教程

    本文通过一个简单的实例完成了完整的PHP+MySQL会员系统功能.是非常实用的一个应用.具体实现步骤如下: 一.会员系统的原理: 登陆-->判断-->保持状态(Cookie或Session ...

  10. python实现决策树

    1.决策树的简介 http://www.cnblogs.com/lufangtao/archive/2013/05/30/3103588.html 2.决策是实现的伪代码 “读入训练数据” “找出每个 ...