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 ...
随机推荐
- windows下安装、卸载mysql服务
将下载下来的mysql解压到指定目录下(如:d:\mysql)安装服务在命令行输入d:\mysql\bin\mysqld -installnet start mysql卸载服务在命令行输入net st ...
- 使用dtc把dtb的反编译为dts
sudo apt-get install device-tree-compiler dtc -I dtb -O dts msm8976-v1.1-qrd.dtb > msm8976-v1.1-q ...
- scrapy框架之CrawlSpider操作
提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...
- Linux Git install
1.介绍 使用Coding管理项目,上面要求使用的git版本为1.8.0以上,而很多yum源上自动安装的git版本为1.7,所以需要掌握手动编译安装git方法. 2.安装git依赖包 yum inst ...
- python学习过程中的踩坑记录<若干,随时更新>
问题1:python中print的连串输出与java不一样? 输入print(code +"+++"); --在代码中写入,界面未报错,但是告诉你不行 会报错,如图: 解决办法: ...
- openstack provider self-service network subnet 创建
- python操作浏览器及截图小结
近期做网页自动化用到内容小结 1.打开浏览器1)打开默认配置的浏览器from selenium import webdriverdriver = webdriver.Firefox()"&q ...
- Mybatis学习4——一对一关联查询方法1--创建实体
创建一个实体继承两个实体之一,另一个实体作为属性 实体1. order package pojo; import java.util.Date; public class Order { privat ...
- Windows Server 2016正式版教程:安装、激活、设置
https://blog.csdn.net/qq_35306193/article/details/77828990 windows-server-2016介绍.安装.激活.设置等 ...
- docker利用Dockerfile来制作镜像
在前面的例子(docker tomcat镜像制作)中,我们从下载镜像,启动容器,在容器中输入命令来运行程序,这些命令都是手工一条条往里输入的,无法重复利用,而且效率很低.所以就需要一 种文件或脚本,我 ...