[刷题] 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 ...
随机推荐
- [Kick Start] 2021 Round A
题目:2021 Round-A . K-Goodness String 签到题,计算当前字符串的 K-Goodness Score ,然后与给出的 K 做差即可. #include <iostr ...
- springboot基础项目搭建(十五篇)
springboot系列一.springboot产生背景及介绍 springboot系列二.springboot项目搭建 springboot系列三.springboot 单元测试.配置访问路径.多个 ...
- CountDownLatch与CyclicBarrier的基本使用
1 概述 CountDownLatch以及CyclicBarrier都是Java里面的同步工具之一,本文介绍了两者的基本原理以及基本使用方法. 2 CountDownLatch CountDownLa ...
- (十三)VMware Harbor 身份验证模式
VMware Harbor 修改Harbor仓库admin用户 参考:https://blog.csdn.net/qq_40460909 https://blog.csdn.net/qq_404609 ...
- Day16_97_IO_FileOutputStream 写入字节流
FileOutputStream 写入字节流 * java.io.OutPutStream -----> java.io.FileOutputStream 将计算机内存中的数据写于磁盘中. * ...
- Webpack的理解以及解决了的问题
一.背景 Webpack 最初的目标是实现前端项目的模块化,旨在更高效地管理和维护项目中的每一个资源 模块化 最早的时候,我们会通过文件划分的形式实现模块化,也就是将每个功能及其相关状态数据各自单独放 ...
- 1.8.7- HTML值label标签
1.label直接进行包裹input就可以了.
- hdu4503 概率
题意: 湫湫系列故事--植树节 Time Limit: 1000/500 MS (Java/Others) Memory ...
- CTB-Locker敲诈者病毒下载器分析
一. 样本基本信息 样本名称:927354529512.scr 样本大小:110592 字节 病毒名称:Win32.Trojan.Ctb-locker.Auto 样本MD5值:3A6D7E551C13 ...
- Linux中的DHCP服务
目录 DHCP DHCP的报文类型 DHCP的部署 DHCP中继(DHCP代理) DHCP DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局 ...