437. Path Sum III
原题:
解题:
思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点
代码如下:
class Solution {
public:
int pathSum(TreeNode* root, int sum)
{
if(!root) return 0;
int count = getPath(root,0,sum) + pathSum(root->left,sum) + pathSum(root->right,sum);
return count;
}
int getPath(TreeNode* node, int target, int sum)
{
if(!node) return 0;
target += node->val;
return (target == sum) + getPath(node->left, target, sum) + getPath(node->right, target, sum);
}
};
以下代码是论坛里看到的,思路差不多,也是递归:
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
path(root,0,sum);
return total;
}
int total;
void incrementTotal(){
this->total++;
}
void path(TreeNode* root,int sum, int target){
if(root != NULL){
checkpath(root,0,target);
path(root->left,0,target);
path(root->right,0,target);
}
}
void checkpath(TreeNode* root,int sum,int target){
if(root == NULL){
return;
}
int check = sum + root->val;
if(check == target){
incrementTotal();
}
checkpath(root->left,check,target);
checkpath(root->right,check,target);
}
};
437. Path 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 ...
- 【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 路径和 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 ...
- 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 ...
随机推荐
- java中序列化的简单认识
一.什么是序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比JVM的生命周期更长.但在现实应用中,就可 ...
- 封装MemoryCache
一.定义一个缓存接口IChace using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- 图片路径中含有中文在jsp下不能正常显示的问题
图片路径中含有中文在jsp下不能正常显示的问题~ 这里其实涉及到get请求编码和url编码的问题. jsp页面: 当路径中存在中文的时候,最简单的解决办法是改变tomcat的编码: 在conf/ser ...
- Masonry基本语法
添加约束的方式: 1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConstraints = NO,即取 ...
- JavaScript之函数,词法分析,内置对象和方法
函数 函数定义 JavaScript中的函数和Python中的非常类似,只是定义方式有点区别. // 普通函数定义 function f1() { console.log("Hello wo ...
- win10 java1.7安装笔记
博主不选择安装C盘,选择在D盘安装,新建Java文件夹,在Java文件夹中新建两个子文件夹,一个命名jdk1.7,一个命名jre,如果不区分目录安装jdk和jre,会导致一些文件的缺失,导致一些错误, ...
- java的Map遍历
java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点 先初始化一个mappublic ...
- day6--二分查找法
二分查找法 我们在使用一个列表的时候,往往需要找到一个元素的位置也就是它的索引,按照一般的情况,肯定是一个一个的找过去,元素多了就是一件麻烦事.. 后来就引进了一个概念:二分查找法 它是根据情况将数据 ...
- idea 控制台行数限制
在本地进行测试时,会出现报错太多idea控制台被限制打印出来的日志被清楚的现象: idea改变控制台打印log限制的方法: 1. 点击 File ->Settings ->editor - ...
- Linux:DNS服务器搭建及配置
1.yum install -y bind bind-chroot bind-utils 2.编辑DNS主配置文件 vim /etc/named.conf 修改如下标红色处即可: options ...