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. OS X 系统,修改hosts文件后不生效的问题

    系统版本 OS X El Capitan,10.11.2 问题描述 业务需要,配置“nexus”为某个ip,如下图更改了 /etc/hosts 文件. 结果在ping的时候,请求并未发到指定ip上. ...

  2. 用 javascript 脚本,网站判读来访者是手机还是电脑

    <script> var system ={}; var p = navigator.platform; system.win = p.indexOf("Win") = ...

  3. 十一、Manipulators

    1. Manipulators是可以用三维进行绘制的的节点,可以将用户行为值化,对其他节点的属性进行modify. 2. 属性值可以通过Channel Box 和Graph Editor进行修改.同其 ...

  4. 组合or继承

    面向对象设计有一个原则“优先使用对象组合,而不是继承”. 下面是两者优缺点的比较: 组 合 关 系 继 承 关 系 优点:不破坏封装,整体类与局部类之间松耦合,彼此相对独立 缺点:破坏封装,子类与父类 ...

  5. Jmeter模拟不同带宽

    Jmeter自带模拟带宽设置,当然前提肯定是你当前的带宽>=你要模拟的带宽,好比你装了个4m的宽带,要模拟100m的带宽,那是做梦 做起来也不难,打开user.properties文件,增加如下 ...

  6. webstorm编辑RN代码提示功能

    需要下载一个文件. 1. 进入你想存储这个文件的目录,按住shift+鼠标右键,选择“在此处打开命令窗口” 2. 在命令窗口中输入  git clone https://github.com/virt ...

  7. Tomcat回收连接

    最近公司一个JDK1.4的老项目升级了JDK1.6后BUG不断,最可恶的连接池被占满. 因为是使用tomcat的连接池所以在config下中添加 <Resource name="jdb ...

  8. Foundation ----->NSDictionary

    /*_______________不可变字(NSDictionary)____________*/          //1.字典的创建     //值(value)     NSArray *arr ...

  9. 控制input框不能更改里面的内容

    <input type="text" disabled="true"/> 这个是给input设置一个属性.控制它可以不能改变里面的内容.已经试过了! ...

  10. C语言 教学实践建议

    这是2016年秋季学期和北京工业大学耿丹学院合作教学的计划. 2016级有四个班,每班大约 32 人,每班配有一个有一定实际工作经验的助教,配合老师把课教好. C语言是一门基础课, 是耿丹学院新生的第 ...