[刷题] 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 ...
随机推荐
- ASP.NET扩展库之Http日志
最佳实践都告诉我们不要记录请求的详细日志,因为这有安全问题,但在实际开发中,请求的详细内容对于快速定位问题却是非常重要的,有时也是系统的强力证据.Xfrogcn.AspNetCore.Extensio ...
- django+x-admin管理后台模板开发管理后台案例(设计部分)
使用django+x-admin管理后台模板搭建管理后台 一.环境需求 1.django:3.1 2.python:3.7 3.x-admin:2.2 4.pycharm:2020.3.2 5.ubu ...
- Tk 的基本概念-组件—Tkinter 教程系列03
Tk 的基本概念-组件-Tkinter 教程系列03 前言 Tk 系列教程: Tkinter教程系列01--引言和安装Tk Tkinter教程系列02--第一个真正的 GUI 程序 通过上一节的程序实 ...
- 简述Java多线程(一)
JAVA多线程 程序:是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念. 进程:是执行程序的一次执行过程,是一个动态的概念,是系统资源分配的单位. 线程是CPU调度和执行的单位. 创 ...
- Leedcode算法专题训练(位运算)
https://www.cnblogs.com/findbetterme/p/10787118.html 看这个就完事了 1. 统计两个数的二进制表示有多少位不同 461. Hamming Dista ...
- [Vue warn]: Unknown custom element: <terminal-process> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
Vue组件注册报错问题 import 不要加{},排查出如果页面引用单个组件的时候不要加上{}中括号,引入多个组件时才能派上用场,中括号去除问题即可解决.
- 《C标准库》学习笔记整理
简介 <C标准库>书中对 C 标准库中的 15 个头文件的内容进行了详细的介绍,包括各头文件设计的背景知识.头文件中的内容.头文件中定义的函数和变量的使用.实现.测试等. 我学习此书的目的 ...
- 使用MyQR模块生成二维码
一.介绍 MyQR 模块是 python 的一个外部库,能够将文本或网址转为二维码,扫了之后就能查看文本或跳转到相应网站. 它是一个外部库,需要导入: pip install -i https://p ...
- 域迁移DA | Learning From Synthetic Data: Addressing Domain Shift for Se | CVPR2018
文章转自:微信公众号「机器学习炼丹术」 作者:炼丹兄(已授权) 联系方式:微信cyx645016617 论文名称:"Learning From Synthetic Data: Address ...
- 1.4.18- HTML之特殊字符标签
上面图片内容简单了解一下,下面我们看一段代码: <!DOCTYPE html> <html lang="en"> <head> <meta ...