LeetCode112 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. (Easy)
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 - root -> val用在递归函数中,这样写最简洁。
代码:
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root == nullptr) {
return false;
}
if(root -> right == nullptr && root -> left == nullptr) {
return sum == root->val;
}
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val) ;
}
};
LeetCode112 Path Sum的更多相关文章
- 第34-1题:LeetCode112. Path Sum I
题目 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [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. ...
- [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 ...
- [LeetCode] Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- Path Sum
需如下树节点求和 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 JavaScript实现 window ...
随机推荐
- 彭亮—Python学习
1.1 Python简单介绍 1.2 安装Python和配置环境 1.配置Python 1.1 下载Python(直接去官网下载就可以) 1.2 安装Python(点解默认安装即可 ...
- NOSQL数据库之 REDIS
NOSQL数据库之 REDIS 一.NOSQL 1.简介 NoSQL ,(Not Only SQL),泛指非关系型数据库. 特点: NoSQL 通常是以key-value形式存储, 不支持SQL语句, ...
- 全景还原报错现场 | 应用实时监控 ARMS 上线用户行为回溯功能
随着前端技术日新月异迅猛发展,为了实现更好的前端性能,最大程度提高用户体验,支持单页应用的框架逐渐占领市场,如众所周知的React,Vue等等.但是在单页应用的趋势下,快速定位并解决JS错误却成为一大 ...
- 用户测评 | EDAS Serverless 上手体验
背景 最初, 是因为对 Serverless 这一概念感兴趣, 所以开始试用阿里云函数计算,使用过程中感受到了函数计算快速.按需付费和弹性伸缩等方面的优势,随后我在天气预报.发送短信等场景下开始了更深 ...
- 【xlwings1】Python-Excel 模块哪家强
Python-Excel 模块哪家强? 0. 前言 从网页爬下来的大量数据需要清洗? 成堆的科学实验数据需要导入 Excel 进行分析? 有成堆的表格等待统计? 作为人生苦短的 Python 程序 ...
- bzoj 1266 [AHOI2006] 上学路线
传送门 传说中的经典容斥+卢卡斯定理+中国剩余定理 题解传送门 //Achen #include<algorithm> #include<iostream> #include& ...
- gomod
package main import "mycore" func main(){ mycore.ShowName() } main.go module mytest requir ...
- JAVA--养成好的编程习惯
原文地址:http://user.qzone.qq.com/1159340782/2 略微牺牲一些性能,换来更好的可读性和可维护性是好的选择.以下不提倡和提倡的写法仅供参考. 代码中不提倡的写法 序号 ...
- 学习线程池源码--ThreadPoolExecutor
1 创建ThreadPoolExecutor ThreadPollExecutor有四个构造函数,但本质上都是调用这一个构造函数. public ThreadPoolExecutor(int core ...
- 有趣的HTML5 Web SQL 数据库
Web SQL 数据库 API 并不是 HTML5 规范的一部分,但是它是一个独立的规范,引入了一组使用 SQL 操作客户端数据库的 APIs. 核心方法 以下是规范中定义的三个核心方法: openD ...