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.

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems
 
解答
其实只要注意叶节点时的处理就可以了。
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 int flag = false;
void DFS(struct TreeNode *root, int sum){
    if(root == NULL){
        return;
    }
    ){
        flag = true;
    }
    DFS(root->left, sum - root->val);
    DFS(root->right, sum - root->val);
}
bool hasPathSum(struct TreeNode* root, int sum) {
    flag = false;
    DFS(root, sum);
    return flag;
}

LeetCode OJ 112. Path Sum的更多相关文章

  1. Leetcode 笔记 112 - Path Sum

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

  2. 【LeetCode OJ】Path Sum II

    Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...

  3. 【LeetCode OJ】Path Sum

    Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...

  4. 【一天一道LeetCode】#112. Path Sum

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. LeetCode OJ 113. Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  6. 【LeetCode】112. Path Sum

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

  7. LeetCode OJ:Path Sum II(路径和II)

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  8. LeetCode OJ:Path Sum(路径之和)

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

  9. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. oracleclient连oracle库 报System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本

    在iis下发布eworkflow+eform+ebiao的代码,访问oracle的数据库,用oracleClient或者oledb的方式连接,有时会报“System.Data.OracleClient ...

  2. Android安装apk时报错:INSTALL_FAILED_NO_MATCHING_ABIS

    问题背景 OS:无关 AS:无关 Genymotion:2.5.2 Virtual Device:Google Nexus 5 - 5.1.0 - API 22 原因分析 CPU架构不符 解决方案 对 ...

  3. 【转】常见的python机器学习工具包比较

    http://algosolo.com/ 分析对比了常见的python机器学习工具包,包括: scikit-learn mlpy Modular toolkit for Data Processing ...

  4. WINDOWS Server2008上部署Oracle10g及oracle SQL语法小记

    首先安装10G客户端 情况一:一般都会安装到一般报错.因为10G是32BIT客户端.而操作系统是64位的.但是不会影响配置监听程序.自主开发的应用程序依然可以运行. 情况二:报错但是配置完监听程序始终 ...

  5. IaaS, PaaS, SaaS 解释

    IaaS.PaaS.SaaS作为云计算三种服务模式,下面会着重介绍他们的定义.作用.功能.对应产品以及他们之间的关系等. 一.定义:     SaaS:Software as a Service,软件 ...

  6. vs2010通过添加资源文件,修改win32创建的窗口

    1.右键项目的“资源”,新建资源,生成.rc文件 2.进入资源视图创建icon或menu等资源 3.退出资源视图,编译这个.rc文件 4.项目源代码中添加如下几个头文件 #include " ...

  7. 腾讯云Centos下Nginx反向代理Apache+Tomcat

    1. 安装Apahce, PHP, MySQL以及php连接mysql库的组件#yum -y install httpd php mysql mysql-server php-mysql     // ...

  8. setFeatureInt、android 自定义标题栏

    Android 自带的toolbar 往往不能很好的的满足我们的个性化要求.因此我们经常使用自定的的标题栏.而Android系统本身也允许我们自定以标题栏. 记录一下,自定义标题栏常遇到的问题.先上效 ...

  9. python转exe的小工具

    其实只是在cxfreeze的基础上加了个壳,做成窗口软件了 使用了pyqt做的界面,软件发布在了开源中国上,可以直接去下面的地址查看 http://git.oschina.net/robocky/py ...

  10. FlASK中的endpoint问题

    先贴一点有关的flask代码,时间有限,我慢慢扩充 以下是flask源码中app.py中add_url_rule的代码. 主要是view_func  -- endpoint -- url 之间的对应关 ...