【easy】112.path sum 113.-----------------
求是否有从根到叶的路径,节点和等于某个值。
/**
* 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) {
return false;
}
if (root->left == NULL && root->right == NULL) {
return sum == root->val;
}
return hasPathSum (root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
};
返回所有和为特定值的路径
return
[
[5,4,11,2],
[5,8,4,5]
]
未完待续
【easy】112.path sum 113.-----------------的更多相关文章
- 【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 ...
- 【easy】437. Path Sum III 二叉树任意起始区间和
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 【LeetCode】666. Path Sum IV 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 【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】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- 【leetcode】Minimum Path Sum(easy)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【一天一道LeetCode】#112. Path Sum
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode❤python】 112. Path Sum
#-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init_ ...
- 【题解】【矩阵】【DP】【Leetcode】Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
随机推荐
- maven压缩js css
maven压缩<plugin> <!-- YUI Compressor Maven压缩插件 --> <groupId>net.alchim31.maven</ ...
- pyspider常见错误
安装完爬虫框架pyspider之后,使用pyspider all 命令,可能会出现以下错误: - Deprecated option 'domaincontroller': use 'http_aut ...
- C# 远程获取图片二进制
直接上代码, 紧做记录. public byte[] GetByteByImgUrl() { System.Net.WebRequest webreq = System.Net.WebRequest. ...
- Powershell 函数中的CmdletBinding()是怎么回事?
参考文章: Don Jones https://technet.microsoft.com/en-us/library/ff677563.aspx powershell 帮助文档: help abou ...
- 使用jquery中$.each()方法来循环一个数据列表
定义和用法 jQuery.each() 函数用于遍历指定的对象和数组. 语法 $.each( object, callback ) 参数 描述 object Object类型 指定需要遍历的对象或数组 ...
- centos7之zabbix3.2的fping监控
zabbix通过fping检测主机网络状态 fping的官方网站:http://www.fping.org/ 官网指定的github的地址:https://github.com/schweikert/ ...
- 企业nginx应用实例(功能拆分记录)
一.默认访问协议强制跳转(http--->https) server { listen ; server_name dannylinux.top www.dannylinux.top; # re ...
- 4月10日java上机任务
1. 一维数组的创建和遍历. 声明并创建存放4个人考试成绩的一维数组,并使用for循环遍历数组并打印分数.要求: (1) 首先按“顺序”遍历,即打印顺序为:从第一个人到第四个人: (2) ...
- Python——列表、元祖、字典 操作方法
一.编码方式占位 1.ASCII码:字母.数字.特殊字符,1个字节占8位 2.Unicode:字母 2字节占16位 / 中文 4字节 占32位 3.UTF8:字母 1字节占8位 / 欧洲 2字节占 ...
- 使用按钮触发element 时间事件 --时间戳
本日 本周 本月 本年 时间按钮 date 组件内添加 pickerOptions2: { shortcuts: [ { text: '今日', onClick(picker) { picker ...