原题:

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. 基于HAProxy+Keepalived高可用负载均衡web服务的搭建

    一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web ...

  2. 备用DNS域名服务器

    DNS:1.34.151.129,域名:www#eliuliang#com, 个人用解析地址,请勿使用.

  3. Android Studio设置自定义字体

    Android Studio设置自定义字体 (1)进入设置页面,File->Settings (2)自定义字体Editor->Colors&Fonts->Font (3)点击 ...

  4. web端文字转语音的几种方案

    最近在开发一个微信排队取号的的系统,其中对于服务员端(管理端) 需要有呼叫功能,即点按钮 就播出"xxx号顾客请就座"的声音. 经过在网上一番搜索研究,web端实现指定文字的语音播 ...

  5. Service 和 IntentService的区别;

    Srevice不是在子线程,在Srevice中做耗时操作一样ANR,然后我们就会用到IntentService,IntentSrevice不但擅长做耗时操作,还有一个特点,用完即走: 在Srevice ...

  6. Linux安装vsftpd组件

    1 安装vsftpd组件 安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件. [root@hadoop1 ~]# yum -y install vsftpd 2 ...

  7. Tomcat启动报错:Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies

    错误代码如下: Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for ...

  8. day6--递归函数

    一递归函数 我们老师经常喜欢讲的一句话就是:人理解函数,神理解递归,那么什么是递归函数? 递归函数:在一个函数里面调用函数本身,也就是说这个函数里面出现了和函数一样的名字 例如: def func(n ...

  9. python学习笔记_week11

    一.RabbitMQ python的queue(生产者消费者模型)分为线程queue(不能跨进程)和进程queue(父进程与子进程进行交互或者同属于同一父进程下多个子进程进行交互),那两个独立的进程间 ...

  10. python中的replace

    replace用于修改列表.元组.字典中的元素, 例子: 1 li = ["alec", " aric", "Alex", "To ...