[刷题] 437 Paths Sum III
要求
- 给出一棵二叉树及一个数字sum,判断这棵二叉树上存在多少条路径,其路径上的所有节点和为sum
- 路径不一定始于根节点,终止于叶子节点
- 路径要一直向下
思路
- 分情况讨论:根节点在路径上(8) / 根节点不在路径上(9-10)
- 递归嵌套递归
实现
1 class Solution {
2 public:
3 int pathSum(TreeNode* root, int sum) {
4
5 if( root == NULL )
6 return 0;
7
8 int res = findPath( root , sum );
9 res += pathSum( root->left , sum );
10 res += pathSum( root->right , sum );
11
12 return res;
13 }
14
15 private:
16 // 根节点在路径上
17 int findPath( TreeNode* node, int num ){
18
19 if( node == NULL )
20 return 0;
21
22 int res = 0;
23 if( node->val == num )
24 res += 1;
25
26 res += findPath( node->left , num - node->val );
27 res += findPath( node->right , num - node->val );
28
29 return res;
30
31 }
32 };
[刷题] 437 Paths Sum III的更多相关文章
- 47. leetcode 437. Path Sum III
437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...
- 437. Path Sum III
原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- Leetcode 437. Path Sum III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- LeetCode 437. Path Sum III (路径之和之三)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- 【easy】437. Path Sum III 二叉树任意起始区间和
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 437. Path Sum III(路径可以任意点开始,任意点结束)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现
1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...
随机推荐
- 发送请求时携带了参数,但是浏览器network没有显示的排错思路
发送请求时携带了参数,但是浏览器network没有显示的排错思路 不知道大家有没有遇到这样子的情况就是发送请求的时候明明携带了参数,但是浏览器的network中就是没有!请看下图和代码! 我发送请求用 ...
- day-05-字典
字典的初识 why: 列表可以存储大量的数据,但数据之间的关联性不强 列表的查询速度比较慢.数量越大查询越慢 what:容器型数据类型:dict how: 数据类型的分类(可变与不可变) 可变(不可哈 ...
- 201873030133-杨子豪 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告
项目 内容 课程班级博客链接 班级博客链接 这个作业要求链接 作业要求链接 我的课程学习目标 了解软件工程的作用与意义,将软件工程与过去所学相结合 这个作业在哪些方面帮助我实现学习目标 体验了结对式的 ...
- 我成为 Microsofti Azure MVP 啦!(ps:不是美国职业篮球)
一,引言 今天是个高兴的日子,早上10点左右收到了来自微软的MVP的礼包的快递,对我来说,这是一件很值得纪念的日子.所以今天就水一篇心得感想吧!!! 我是年前2月9号开始申请MVP的,经历了差不多1个 ...
- Kafka核心技术与实战,分布式的高性能消息引擎服务
Kafka是LinkedIn开发并开源的一套分布式的高性能消息引擎服务,是大数据时代数据管道技术的首选. 如今的Kafka集消息系统.存储系统和流式处理平台于一身,并作为连接着各种业务前台和数据后台的 ...
- 【笔记】《Redis设计与实现》chapter21 排序
chapter21 排序 21.1 SORT<key> 命令的实现 // 用于保存被排序值及其权重的结构 typedef struct _redisSortObject { // 被排序键 ...
- Spring Boot demo系列(十):Redis缓存
1 概述 本文演示了如何在Spring Boot中将Redis作为缓存使用,具体的内容包括: 环境搭建 项目搭建 测试 2 环境 Redis MySQL MyBatis Plus 3 Redis安装 ...
- IDEA xml 注解快捷键
注释:CTRL + SHIFT + / 撤销注释:CTRL + SHIFT + \
- Ugly Numbers UVA - 136
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...
- Java设计模式(一):设计模式概述、UML图、设计原则
1 设计模式概述 1.1 软件设计模式的产生背景 "设计模式"最初并不是出现在软件设计中,而是被用于建筑领域的设计中. 1977年美国著名建筑大师.加利福尼亚大学伯克利分校环境结构 ...