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. jquery的blur之后,focus获取不到焦点的解决办法

    一:很多童鞋可能会遇到这种情况: jquery的blur之后,focus获取不到焦点. 二:使用场景: 用户填写信息的时候,若邮箱为空,则提示 请填写邮箱,并将光标置于填写邮箱的文本框里,方便用户的再 ...

  2. HEVC学习之二CTU, CU, CTB, CB, PB, TB

    在H264标准中,编码层的核心是宏块,一个宏块大小为16X16,包含一个16X16的亮度块,以及对于常用的4:2:0采样格式来说还包含两个8X8的色度块.相对应的在HEVC中类似的结构为编码树单元(C ...

  3. SplendidCRM中给来自EditView中的listbox控件设置选中值或数据源

    DropDownList list = this.findContol("aas") as DropDownList;list.DataSource = new DataTable ...

  4. CentOS 7虚拟机下模拟实现nginx负载均衡

    以CentOS 7为例,我们模拟实现nginx来处理静态资源,apache来处理php 1.首先我们来安装nginx # wget  http://nginx.org/packages/centos/ ...

  5. 北京易信软科信息技术有限公司 问卷调查管理系统V2.0

    北京易信软科信息技术有限公司 问卷调查管理系统V2.0 支持题目模板配置.题型模板配置.选项模板配置,报表查询功能配置 按月建表功能 运用java开发.velocity技术实现页面加载功能,高性能,高 ...

  6. C#对DBF文件的操作

    protected void Page_Load(object sender, EventArgs e) { System.Data.Odbc.OdbcConnection conn = new Sy ...

  7. html尖角提示框的实现

    <style type="text/css">        .box {            background-color: #bebf22;          ...

  8. ConnectionString属性尚未初始化

    问题前因:使用动软代码生成的三成模板然后复制到相应的类库 动软生成的 sql帮助类 推荐的是DBsqlhelp 期间引用了:BLl层:Maticsoft.Common.dll DAl层:Maticso ...

  9. 总结一些关于操作数据库是sql语句还是存储过程问题

    总结一些关于操作数据库是sql语句还是存储过程问题 程序中,你跟数据的交互,需要向数据库拿数据.更改数据库的数据等,这些操作,本身不是程序完成的,而是程序发命令给数据库去做的,不管是通过sql语句方式 ...

  10. highcharts 柱状图 动态加载

    highcharts柱状图动态加载 (1):导入样式 <script type="text/javascript" src="<%=request.getCo ...