原题:

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的更多相关文章

  1. 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 ...

  2. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. leetcode 437 Path Sum III 路径和

      相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...

  7. 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 ...

  8. 【easy】437. Path Sum III 二叉树任意起始区间和

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. 437. Path Sum III(路径可以任意点开始,任意点结束)

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

随机推荐

  1. Angular2 入门详解

    AngularJS 2 快速入门 npm是什么?   npm其实是Node.js Package Manager的简称,是Node.js包管理工具(package manager) 安装Node.js ...

  2. 怎么查看SAS到期时间

    通过以下命令,可以查看SAS到期时间: proc setinit; run;

  3. 用户授权的Sql脚本

    正文 要想成功访问 SQL Server 数据库中的数据, 我们需要两个方面的授权: 获得准许连接 SQL Server 服务器的权利: 获得访问特定数据库中数据的权利(select, update, ...

  4. elasticsearch 外网访问9200端口访问

    可以访问127.0.0.1:9200,但不能访问 公网IP:9200 后面ip就是127.0.0.1的局域网ip,如何解决? 修改配置文件 config/elasticsearch.yml netwo ...

  5. Java - 25 Java 包(package)

    Java 包(package) 为了更好地组织类,Java提供了包机制,用于区别类名的命名空间. 包的作用 1 把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用. 2 如同文件夹一样,包 ...

  6. 将16进制的颜色转为rgb颜色

    在前端面试过程中,常常会遇到这样一种类型的题目: 使用js将16进制的颜色值转为rgb颜色! 反而在项目中,不怎么遇到这种问题,也很少有这种需求的项目. 但毕竟面试中常常遇到,我自己在之前的面试的时候 ...

  7. python中使用tabula爬取pdf数据并导出表格

    Tabula是专门用来提取PDF表格数据的,同时支持PDF导出CSV.Excel格式. 首先安装tabula-py: tabula-py依赖库包括Java.pandas.numpy所以需要保证运行环境 ...

  8. 【Jmeter自学】Jmeter脚本录制(二)

    ==================================================================================================== ...

  9. 《图像处理实例》 之 Voronoi 图

    Voronoi 图的设计 以下的改进是http://www.imagepy.org/的作者原创,我只是对其理解之后改进和说明,欢迎大家使用这个小软件! 如有朋友需要源工程,请在评论处留邮箱! 说明:类 ...

  10. 将控制台信息重新导向到JTextArea

    package com.function; import java.io.FileOutputStream; import java.io.IOException; import java.io.Ou ...