【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 ...
随机推荐
- NTT板子
不说别的. 这份NTT跑得比FFT快,不知道为什么. 以下代码针对\(10^5\)的数据范围. #include<cstdio> #include<vector> #inclu ...
- PHP获取项目所有控制器方法名称
PHP获取项目所有控制器方法名称 //获取模块下所有的控制器和方法写入到权限表 public function initperm() { $modules = array('admin'); //模块 ...
- opentack-openstack组件及功能(1)
一. OpenStack各组件间的关系 图22.1 OpenStack各组件间的关系 1.基础管理服务包含Keystone,Glance,Nova,Neutron,Horizon五个服务 (1)Key ...
- 利用Python中SocketServer 实现客户端与服务器间非阻塞通信
利用SocketServer模块来实现网络客户端与服务器并发连接非阻塞通信 版权声明 本文转自:http://blog.csdn.net/cnmilan/article/details/9664823 ...
- es6 模本字符串拼接方法 ``
1.字符串拼接 可以使用 es6 ` ` 配合 ${xxx} 具体操作上代码 <!DOCTYPE html> <html lang="en"> < ...
- git 回退各种场景操作
在git的一般使用中,如果发现错误的将不想提交的文件add进入index之后,想回退取消,则可以使用命令:git reset HEAD <file>...,同时git add完毕之后,gi ...
- [BZOJ 3745] [COCI 2015] Norma
Description 给定一个正整数序列 \(a_1,a_2,\cdots,a_n\),求 \[ \sum_{i=1}^n\sum_{j=i}^n(j-i+1)\min(a_i,a_{i+1},\c ...
- redis持久化和主从同步
redis持久化rdb与aof 简介 Redis是一种内存型数据库,一旦服务器进程退出,数据库的数据就会丢失,为了解决这个问题,Redis提供了两种持久化的方案,将内存中的数据保存到磁盘中,避免数据的 ...
- P1313 计算系数 HMR大佬讲解
今天,HMR大佬给我们讲解了这一道难题. 这道题明显的二项式定理,自然想到了要用到杨辉三角了.基本思路就是先用for循环求出杨辉三角,这样就求出了x的n次方的系数和y的m次方的系数. 这是大佬的AC代 ...
- Help Me Escape ZOJ - 3640
Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth ...