`

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

 

欢迎关注微信公众号——计算机视觉:

分享到:
评论

相关推荐

    Java程序的设计实用教程(第2版)-ans.doc

    } if(sum==i) System.out.println("完数:"+i); } } } 7. public class Xiti7 { public static void main(String args[]) { int n=1,i=1,jiecheng=1; long sum=0; while(true) { jiecheng=1; for(i=1;i;i++){ ...

    cpp-算法精粹

    Binary Tree Maximum Path Sum Populating Next Right Pointers in Each Node Sum Root to Leaf Numbers LCA of Binary Tree 线段树 Range Sum Query - Mutable 排序 插入排序 Insertion Sort List 归并排序 Merge ...

    LeetCode最全代码

    371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...

    C++实现查找二叉树中和为某一值的所有路径的示例

    算法: FindPath(BTree * root,int sum,int target,Stack * s) 用来计算,sum为栈中的元素的和,target为目标值。 到达一个节点之后计算当前节点和sum的和,如果为target,输出路径返回,如果大于target,则直接返回...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    C++ : 编号 题目名 操作 1 Two Sum 4 Median of Two Sorted Arrays 5 Longest Palindromic Substring 8 String to Integer 11 Container with Most Water 14 Longest Common Prefix 15 Three Sum 16 Three Sum ...

    EhLib 6.3 Build 6.3.176 Russian version. Full source included.

    Delphi/C++ Builder IDE. The program creates folders to keep EhLib binary and other requared files, copies requared files to created folders, compiles packages, register packages in IDE and write ...

    leetcode分类-LeetCode:力码

    C++,有详细思路解释 python,部分有解释 Java,部分有解释 Java Associated Documents and Resources Peter norvig神牛Python代码写的很飘逸,果然是有LISP内功的人! 本书涉嫌抄袭leetcode,特此注明 题目分类 from ###...

    windows dos环境下直接运行linux命令集合

    c++filt.exe kill.exe sha384sum.exe cat.exe ld.exe sha512sum.exe cc-3.exe less.exe shmtool.exe cc.exe lessecho.exe shred.exe chcon.exe lesskey.exe shuf.exe chgrp.exe link.exe size.exe chmod.exe lkbib....

    路径和:leet码问题的一种解决方案,用于查找树节点中是否存在给定的数字求和

    路径和解决leet码问题的方法,以查找树节点总和中是否存在给定的数字一个简单... 运行时间:4毫秒,比Path Sum的C ++在线提交的99.88%快。 内存使用:21.3 MB,少于Path Sum的C ++在线提交的96.61%。 德拉吉·吉达尔

    EhLib 9.1.024

    Delphi/C++ Builder IDE. The program creates folders to keep EhLib binary and other requared files, copies requared files to created folders, compiles packages, register packages in IDE and write ...

    EhLib5.0.13 最新的ehlib源码

    through the search PATH (i.e. DOS "PATH" environment variable; for example, in the Windows\System directory). Add, (if needed) 'EhLib directory' in Tools->Environment Options->Library-> Library Path...

    戳气球leetcode-Leetcode_notes:C++中的leetcode解决方案

    path在局部(不需pop)、在全局(需要pop,但是,有2个测试用例,你不知道pop多少) [129.所有路径:根到叶子节点数字之和] 保存所有路径为字符串:backhand_index_pointing_right:数字:backhand_index_pointing_...

    64. 最小路径和

    链接:https://leetcode-cn.com/problems/minimum-path-sum 思路 这个题目和120. 三角形最小路径和很像,解法是类似的。 所以这里直接通过自底向上动态规划的方式来求解。沿着移动方向相反的方向,即从右下角开始...

    EhLib 8.0 Build 8.0.023 Pro Edition FullSource for D7-XE8

    Delphi/C++ Builder IDE. The program creates folders to keep EhLib binary and other requared files, copies requared files to created folders, compiles packages, register packages in IDE and write ...

    刷leetcode不用stl-3-leetcode-everyday:每天3个leetcode!!!

    1、two-sum 能同时获取元素和index的方法是使用enumerate() 思路:从第一个元素开始,遍历,求每个位置上的差值保存到dict中,如果在接下来的元素在dict中出现,返回下标。。。真牛逼! 7、Reverse integer 字符串...

    浙江大学ACM题解/ZJU 题型分类

    1049 I Think I Need a Houseboat 简单题 1028 Flip and Shift 简单题,可以DP/BFS/……,但是实际上有数学方法可直接判断出来 1026 Modular multiplication of polynomials 简单题,有比较简单的好算法 1307 ...

    全面的算法代码库

      这里有各种算法的C++代码,任何人可以在自己的任何程序中使用,欢迎大家指出代码中的错误以及有待改进的地方。   本仓库内所有代码的授权方式为Unlicense,大家如果使用我的代码开发自己的软件挣了大钱,或是...

    leetcode:C,C ++,Java和Python的LeetCode解决方案

    $ cd /path/to/leetcode/c/ # /path/to/leetcode/cpp/ $ mkdir build $ cd build $ cmake .. $ cd .. CMake将自动生成buildsystem文件并下载 。 注意:添加新的单元测试后,请务必重新加载CMake。 要构建,请使用--...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    + added ability to set any custom page order for printing (i.e. 3,2,1,5,4 ) + [enterprise] added variables "AUTHLOGIN" and "AUTHGROUP" inside the any report + [enterprise] now any report file can be ...

    ehlib_vcl_src_9_3.26

    Delphi/C++ Builder IDE. The program creates folders to keep EhLib binary and other requared files, copies requared files to created folders, compiles packages, register packages in IDE and write ...

Global site tag (gtag.js) - Google Analytics