给一个目标值,判断一棵树从根到叶子是否至少有一条路径加起来的和等于目标值

比较典型的深度优先算法。

引入一个全局变量bResult, 一旦找到一条,就不再搜索其他的了。

class Solution {
public:
void helper(TreeNode* cur, int sum, int target, bool& bResult){
if(bResult)
return; //a(cur)
//lk("root",cur)
//a(sum)
//a(target)
//dsp
if(!cur->left && !cur->right){
bResult|= target-sum==cur->val;
} if(cur->left)
helper(cur->left, sum+cur->val, target, bResult); if(cur->right)
helper(cur->right, sum+cur->val, target, bResult); } bool hasPathSum(TreeNode* root, int sum) {
if(!root)
return false; if(!root->left && !root->right){
return sum==root->val;
} //ahd(root)
bool bRet=false;
//a(bRet) if(root->left)
helper(root->left, root->val, sum, bRet); if(root->right)
helper(root->right, root->val, sum, bRet); //dsp
return bRet; }
};

程序运行动态演示 http://simpledsp.com/FS/Html/lc112.html

LeetCode 112. Path Sum 动态演示的更多相关文章

  1. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  2. [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 all ...

  3. [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 all ...

  4. [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)

    Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...

  5. 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 all ...

  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 all ...

  7. leetcode 112 Path Sum ----- java

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

  8. Java [Leetcode 112]Path Sum

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

  9. [Leetcode]112. Path Sum -David_Lin

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

随机推荐

  1. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  2. 微信JS-SDK选择图片遇到的坑

    微信JS-SDK选择图片遇到的坑 有个需求要在微信企业号里面做开发,有个功能是选择图片,使用input标签肯定是不管用了,Android手机上不能多选,所以使用了微信的JS-SDK提供的相关API,这 ...

  3. JavaScript、ES6中的类和对象

           面向对象可以用于描述现实世界的事物,但是事物分为具体的(特指的)事物和抽象的(泛指的)事物. 面向对象思维的特点: 1.抽取(抽象)对象共有的属性和行为组织(封装)成一个类(模板) 2. ...

  4. iOS开发笔记1

    1.在堆上模拟函数调用栈 背景: 在看算法书时候, 很多地方提到要谨防递归的栈溢出问题. 分析: 递归调用时候, 有可能出现非常深的函数调用. 对于每次的函数调用, 都需要将函数体内的局部变量保存在栈 ...

  5. 你不知道的hostname命令

    一般hostname可以获取主机名,但是hostname实际上可以做更多的事情. 让我们先来看看它的帮助. Usage: hostname [-b] {hostname|-F file} set ho ...

  6. Tarjan 复习小结

    总算把这几个东西策清楚了. 在\(Tarjan\)算法里面,有两个时间戳非常重要,一个是\(dfn\),意为深度优先数,即代表访问顺序:一个是\(low\),意为通过反向边能到达的最小\(dfn\), ...

  7. selenium 自动化的坑(5)

    这次要说的自动化坑是关于<a>标签的,话不多说,先上图: 这个表面上看起来是个输入框,操作的时候需要鼠标先悬停,才会出来下面的选项,刚开始我尝试直接点击,结果当然是失败的了. 注意:我的操 ...

  8. File类常用的方法与字节流类方法简介

    File类常用的方法 获取功能的方法 public String getAbsolutePath() :返回此File的绝对路径名字符串. public String getPath() :将此Fil ...

  9. plafrom SDK

    { //http://www.alipay-seller.mpymnt.com/node/82 //https://blog.csdn.net/xiaopingping1234567/article/ ...

  10. ht-4 hashmap特性

    一.hashmap底层原理: hashmap调用默认构造方法会产生一个默认底层是长度为16的Entry数组,首先调用key的hasCode()方法来得到一个整数, int hash = hash(ke ...