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 andsum = 22, 5 / 

4 8 / / 

11 13 4 / \ 

7 2 1 return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.

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 hasPathSum2(TreeNode *root, int sum) {
if(NULL == root) return false;
if(NULL == root->left && NULL == root->right && sum == root->val) return true;
if(NULL != root->left && hasPathSum(root->left,sum-root->val)) return true;
if(NULL != root->right && hasPathSum(root->right,sum-root->val)) return true;
return false;
} bool hasPathSum(TreeNode *root, int sum) {
if(NULL == root) return false;
if(NULL == root->left && NULL == root->right && sum == root->val) return true;
return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val);
}
};

path-sum leetcode C++的更多相关文章

  1. 【LeetCode】Path Sum ---------LeetCode java 小结

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  2. Binary Tree Maximum Path Sum leetcode java

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

  3. Binary Tree Maximum Path Sum - LeetCode

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  4. Minimum Path Sum [LeetCode]

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

  5. Path Sum [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for bin ...

  6. Minimum Path Sum——LeetCode

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

  7. Minimum Path Sum leetcode java

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

  8. Path Sum leetcode java

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. Binary Tree Maximum Path Sum [leetcode] dp

    a(i):在节点i由于单边路径的最大结束 b(i):在节点i路径和 a(i) = max{ i->val, i->val + max{a(i->left), a(i->righ ...

  10. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

随机推荐

  1. webgl 图像处理2---图像传输

    webgl 图像处理 webgl 不仅仅可以用来进行图形可视化, 它还能进行图像处理 图像处理2---图像传输 之前已经进行了点和 uv 数据的传输 webgl 进行图形处理的第二步: 传输图片到 G ...

  2. php获取纳秒方法

    PHP不提供精度高于微秒的函数. 可以使用system功能,直接从机器中获得的价值,如果你运行的是Linux: $nanotime = system('date +%s%N');

  3. Jmeter系列(30)- 性能指标(3) | 性能指标峰值

    性能指标峰值 简述 彻底理解了性能指标(1)(2)的内容,这一篇随笔其实就不用看了,而且大家也能猜到这一篇内容是啥:二八原则 性能指标不要硬性的往那些性能指标上去靠,要根据业务来,熟悉业务,明白了解你 ...

  4. javascript wchar_t 宽字符 转化为 ascii字符码数组

    String.prototype.charCodeAt String.fromCharCode() String.prototype.toUtfArray = function() { return ...

  5. 如何把springboot项目部署到tomcat上

    前言: 开始以为打包springboot项目为war包丢到tomcat上的webapps下面就可以访问controller层的路径了,可是调用接口却报404的错误,而打开8080的主页,不加路径却可以 ...

  6. 【Markdown】Shell命令高亮显示

    [问题]shell命令,黏贴到简书的代码块上,#后面的命令显示成被注释掉的效果 image.png [目的]高亮显示shell命令 [方案1]在代码块标示符后,加上此代码块所用的语言名(请注意要用小写 ...

  7. Xamarin Android使用自签名证书

    背景 项目中后台web服务部署成https服务时,需要使用SSL证书,如果我们不使用公共的CA时,怎么办? 不仅如此,因为是小项目,App应用主要是小范围使用,此时只有IP地址,根本没有域名,怎么办? ...

  8. cron表达式的双重人格:星期和数字到底如何对应?

    写在前面 cron在希腊语中是时间的意思,而cron表达式(cron expression)则是遵循特定规则,用于描述定时设置的字符串,常用于执行定时任务.本文总结了不同环境(如平台.库等)下,cro ...

  9. T-SQL——数据透视和逆透视

    目录 0. 测试数据集及说明 0.1 准备测试数据 0.2 对一维表和二维表理解 1. 透视转换 1.1 使用标准SQL进行数据透视 1.2 使用T-SQL中pivot函数进行数据透视 1.3 关于 ...

  10. react之组件数据挂在方式

    1.属性(props) 组件间传值,在React中是通过只读属性 props 来完成数据传递的. props:接受任意的入参,并返回用于描述页面展示内容的 React 元素. import React ...