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 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.
思路:
根节点到叶子节点的路径的所有值如果有与sum一样的,则返回true;否则返回false。
如果root为NULL,直接返回false;如果根节点无子叶节点 ,且根节点的值和sum相等,返回true;递归调用,分别判断左节点和右节点,如果有一个满足情况的,就返回为true。
代码:
/*** Definition for a binary tree node.* 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==NULL){//如果root为NULL,直接返回falsereturn false;}if(!(root->left||root->right)&&root->val==sum){//如果根节点无子叶节点 ,且根节点的值和sum相等,返回truereturn true;}//递归调用,分别判断左节点和右节点,如果有一个满足情况的,就返回为truereturn hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);}
};
输出结果: 12ms
LeetCode 112. Path Sum相关推荐
- [勇者闯LeetCode] 112. Path Sum
[勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...
- leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)
题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...
- 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 ...
- [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 ...
- [leetcode] Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- LeetCode 113. Path Sum II
113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum eq ...
- leetcode python3 简单题112. Path Sum
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十二题 (1)题目 英文: Given a binary tree and ...
- 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 given ...
- LeetCode Minimum Path Sum(动态规划)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right wh ...
最新文章
- 前四次作业--个人总结
- WPF与Win32互操作
- 对传入的值,转成整数
- Logtail提升采集性能
- 为什么拼多多没有购物车?
- 希望找到计算机系的好期刊的影响因子的排序?并作为选刊依据?选SJR就足够了
- [Java开发]打印当前路径到控制台
- “约见”面试官系列之常见面试题之第一百零七篇之vue的作用(建议收藏)
- System.Web.HttpContext.Current.Session为NULL解决方法
- 云+X案例展 | 金融类:荣之联助力君康人寿构建新一代数据中心
- 数组长度属性以及JavaScript中的示例
- java的数列极差_[hoj]数列极差问题 | 学步园
- 遥感技术在水利行业中的应用
- 1.2 批量生成MySQL建表语句
- VMware ESXi 6.7安装过程介绍
- 尚德机构推出2019版CPA系列纸质书 配套小程序刷题
- MySQL燕十八老师课程笔记:第十六课:MySQL各个函数
- 点计算机图标就自动关机了,为什么点计算机的关机图标不能自动关机
- 微信OAuth授权获取用户OpenId-JAVA(个人经验)
- 自然辩证法复习题 1
热门文章
- php mysql 日期查找_PHP / MySQL存储和搜索日期
- C++:new[]和delete[]一定要配对使用吗?
- OpenGL:读取图片显示
- Scala里Map()集合
- 前端框架:AntdUI 文档入门
- mui框架mui-active高亮当前栏目 - 代码说明
- 关于多个router-view视图组件,渲染同一页面
- IE浏览器兼容性处理与提示
- 苏州大学计算机组成与结构,苏州大学计算机组成结构期末.docx
- python怎么播放本地录音_Python播放音频与录音