题目描写叙述:

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.

代码:

bool Solution::hasPathSum(TreeNode *root, int sum)
{if(root == NULL)return false;if(root->left == NULL && root->right == NULL)return root->val - sum;elsereturn hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val);
}
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5412727.html,如需转载请自行联系原作者  

LeetCode:Path Sum相关推荐

  1. LeetCode Path Sum III(前缀和)

    问题: 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二叉树 ...

  2. LeetCode Path Sum II(dfs或者bfs)

    问题:给出一个树和一个数,求出从根结点到叶子结点路径和等于这个数的所有情况 思路: 1.深度优先搜索,在到达一个深度结点时,判断是否是叶子结点,并且判断和是否等于要求的数.如果满足,说明是满足条件的一 ...

  3. [Leetcode] 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 ...

  4. functionclass[LeetCode]Path Sum II

    在本篇文章中,我们主要介绍functionclass的内容,自我感觉有个不错的建议和大家分享下 每日一道理 只有启程,才会到达理想和目的地,只有拼搏,才会获得辉煌的成功,只有播种,才会有收获.只有追求 ...

  5. LeetCode: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 | 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 ...

  7. Leetcode | Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  8. [LeetCode]113.Path Sum II

    [题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the giv ...

  9. [Leetcode] Binary Tree Maximum Path Sum

    这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...

最新文章

  1. svd协同过滤java实现_利用 SVD 实现协同过滤推荐算法
  2. python语言入门m-「数据挖掘入门系列」Python快速入门
  3. spring bean中scope=prototype“的作用
  4. C++const的作用与使用
  5. My FioriTest navigation from master page to detail page
  6. 接口应用练习题(用接口的方式实现某网上商城的网上支付功能)
  7. python里的join方法_python中join()方法介绍
  8. Spring @Lazy批注用例
  9. C++ override 关键字用法
  10. STM32项目(二) —— 车灯测试台架
  11. WinLogon事件通知包编程
  12. css高清动图,CSS3+PNG实现GIF动画效果
  13. 讲述一个自学七年Python编程的码农人生
  14. 管理者每天要做的十件事
  15. 在 Linux 虚拟机中手动安装或升级 VMware Tools
  16. 分区助手服务器,分区助手专业版
  17. 书店管理系统课程设计( sql server+python)
  18. 关于杭州电子科技大学毕业论文格式设置
  19. s3c2440 linux3.2.65 uda134x声卡卡顿,杂音修复
  20. python 微信图文消息接口_用Python实现微信公众号API素材库图文消息抓取

热门文章

  1. php语句创建数据表,用mysql语句创建数据表详细教程
  2. winform 的exe为什么移动之后不能使用_C++MyDock配置要求、所需运行库、常见问题 初次使用必看...
  3. java expression 强制出现_Java中带有强制括号对的单行循环
  4. java类中的static块_java类中static代码块的执行次数
  5. SqlServer保留几位小数的两种做法
  6. spring boot 启动 Error:java: Compilation failed: internal java compiler error 解决办法
  7. 不能使用 float 和 double 来表示金额等精确的值
  8. 比较有名的CSS,优雅地写css
  9. php日期控件calendar.js,轻量级的原生js日历插件calendar.js使用指南
  10. java中ant是干什么的_Java_Ant详解(转载)