题目:

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. Jquery设置select控件指定text的值为选中项

    <select name="streetid" id="streetid"> <option value="4">北 ...

  2. EXT格式误删除恢复

    http://hatemysql.com/ 1.从/proc文件系统恢复数据#lsof |grep -i deletecat 11791 root 1w REG 253,0 94 1048589 /h ...

  3. cmd中无法运行svn命令

    Svn 不是内部或外部命令,也不是可运行的程序 解决方法: 增加“svn安装目录/bin”,例如:C:\Program Files\TortoiseSVN\bin

  4. 批量关闭 WordPress 的 Pingback 和 Trackback 功能

    方法很简单,WordPress 后台即可实现,在设置-讨论中把"接收来自外部博客的引用通告(Pingback 和 Trackback)"这一项勾选去掉,保存设置.这样,以后新增的文 ...

  5. QQl聊天消息

    Activity: package com.zzw.qqchat; import java.util.ArrayList; import java.util.HashMap; import andro ...

  6. Laravel 5 基础(七)- Eloquent (laravel 的ORM)

    我们来生成第一个模型 php artisan make:model Article #输出 Model created successfully. Created Migration: 2015_03 ...

  7. MYSQL数据库表中字段追加字符串内容

    $sql="update parts set p_notes=concat(p_notes,'{$p_notes}') where p_id={$p_id}"; parts为表名 ...

  8. Winform开发几个常用的开发经验及知识积累(一)

    本人做Winform开发多年,孜孜不倦,略有小成,其中收集或者自己开发一些常用的东西,基本上在各个项目都能用到的一些开发经验及知识积累,现逐步介绍一些,以飨读者,共同进步. 1.窗口[×]关闭按钮变为 ...

  9. python xml包使用记录

    <?xml version="1.0" encoding="utf-8" ?> <request> <functionID> ...

  10. Web Design:给实验室UI们的一堂课(下)

    [讲稿]From top to down,自顶向下哈,首部栏.导航栏之后一般是页面的主模块,也就是Body部分,这一块儿才是你网站的核心内容,文章.新闻.动态.数据.图表.相册等都是在这儿体现出来.在 ...