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

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

引入一个全局变量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. spring注解之@Scope

    转自:https://blog.51cto.com/4247649/2118351 作者:知了123 主要从以下几方面来介绍一下@Scope注解 @Scope注解是什么 @Scope注解怎么使用 @S ...

  2. asp.net Swiper 轮播动画

    原文:https://blog.csdn.net/qq_39656138/article/details/90451289 官网:https://www.swiper.com.cn/api/index ...

  3. idea 创建java web项目ssm-gradle

    环境准备:jdk1.8+tomcat8+idea+gradle 1.创建项目SSM 使用gradle创建项目,按照提示如下   image.png 输入项目名称,组名   image.png   im ...

  4. 如何设置移动端的tab栏

    这是添加tab栏的代码: {                     "id": "tabBar1",                     "st ...

  5. Taro -- 使用 Redux 来进行全局变量的管理

    前言 Redux是JavaScript 状态容器,提供可预测化的状态管理.一般来说,规模比较大的小程序,页面状态,数据缓存,需要管理的东西太多,这时候引入Redux可以方便的管理这些状态,同一数据,一 ...

  6. P2617 Dynamic Rankings(待修改区间第k大)

    题目链接:https://www.luogu.org/problemnew/show/P2617 题目: 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的 ...

  7. java并发学习--第三章 线程安全问题

    线程的安全问题一直是我们在开发过程中重要关注的地方,出现线程安全问题的必须满足两个条件:存在着两个或者两个以上的线程:多个线程共享着共同的一个资源, 而且操作资源的代码有多句.接下来我们来根据JDK自 ...

  8. rocketmq启动broker内存占用过大的问题

    解决方法: 修改broker启动脚本runbroker.sh里面的jvm参数 JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g" ...

  9. sys模块 json pickle模块

    # sys模块# import sys# sys.path# sys.argv# sys.exit() # 脚本退出# print('[%s]'%('#'*1))# print('[%s]'%('#' ...

  10. tensorflow图像处理函数(1)

    1.tensorflow中对jpeg格式图像的编码/解码函数: import matplotlib.pyplot as plt import tensorflow as tf image_raw_da ...