【easy-】437. Path Sum III 二叉树任意起始区间和
/**
* 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:
int pathSum(TreeNode* root, int sum) {
if (root == NULL)
return ;
return Sum(root, , sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
}
private:
//pre为前面节点的和,cur为前面加上现在遍历到的节点;
int Sum(TreeNode* root, int pre, int sum){
if (root == NULL)
return ;
int cur = pre + root->val; return (cur == sum) + Sum(root->left, cur, sum) + Sum(root->right, cur, sum);
}
};
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1
Return 3. The paths that sum to 8 are:
1.  5 -> 3
2.  5 -> 2 -> 1
3. -3 -> 11
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
【easy-】437. Path Sum III 二叉树任意起始区间和的更多相关文章
- 【easy】437. Path Sum III 二叉树任意起始区间和
		
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
 - 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] 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 ...
 - [LeetCode] 437. Path Sum III 路径和 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 路径和
		
相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
 
随机推荐
- HashMap源码分析(基于jdk8)
			
我们知道在jdk7中HashMap的实现方式是数组+链表.而在jdk8中,实现有所变化,使用的是数组+链表+红黑树实现的. 当链表长度达到8时转化为红黑树. static final int TREE ...
 - Windows下安装配置MongoDB
			
Windows下安装配置MongoDB 一,介绍 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB ...
 - 【MySQL 读书笔记】SQL 刷脏页可能造成数据库抖动
			
开始今天读书笔记之前我觉得需要回顾一下当我们在更新一条数据的时候做了什么. 因为 WAL 技术的存在,所以当我们执行一条更新语句的时候是先写日志,后写磁盘的.当我们在内存中写入了 redolog 之后 ...
 - 《AutoCAD Civil 3D .NET二次开发》勘误2
			
4.6.3节中代码: 原代码: 06 pdo.Keywords.Add("Pi", "Pi", "派<Pi>"); 07 pdo ...
 - java文件运行的过程
			
javac .java——>编译成.class文件(字节码) 参考: https://www.cnblogs.com/yxwkf/p/3855363.html https://www.jians ...
 - MySQ数据备份
			
MySQL备份概述 问题:备份和冗余有什么区别? 备份:能够防止由于机械故障以及人为操作带来的数据丢失,例如将数据库文件保存在了其它地方. 冗余:数据有多份冗余,但不等于备份,只能防止机械故障带来的数 ...
 - 将gbk字符串转换成utf-8,存储到注册表中后,再次从注册表读取转换成gbk,有问题!!!
			
char *a = "新2新"; printf("gbk:'%s'\n", a); int ii; ; ii < strlen(a); ii++) { p ...
 - CMakeList.txt(2):CMakeLists.txt编写规则
			
#project namePROJECT(test_math) 指定生成的工程名为test_math #head file path INCLUDE_DIRECTORIES(includ ...
 - PageRank算法初探
			
1. PageRank的由来和发展历史 0x1:源自搜索引擎的需求 Google早已成为全球最成功的互联网搜索引擎,在Google出现之前,曾出现过许多通用或专业领域搜索引擎.Google最终能击败所 ...
 - .Net Core在Centos7上初体验
			
本文主要内容是简单介绍如何在centos7上开发.Net Core项目,在此之前我们首先了解下.Net Core的基本特性. 1 .Net Core和.Net FrameWork的异同 1.1 .Ne ...