要求

  • 给出一棵二叉树及一个数字sum,判断这棵二叉树上存在多少条路径,其路径上的所有节点和为sum
  • 路径不一定始于根节点,终止于叶子节点
  • 路径要一直向下

思路

  • 分情况讨论:根节点在路径上(8) / 根节点不在路径上(9-10)
  • 递归嵌套递归

实现

 1 class Solution {
2 public:
3 int pathSum(TreeNode* root, int sum) {
4
5 if( root == NULL )
6 return 0;
7
8 int res = findPath( root , sum );
9 res += pathSum( root->left , sum );
10 res += pathSum( root->right , sum );
11
12 return res;
13 }
14
15 private:
16 // 根节点在路径上
17 int findPath( TreeNode* node, int num ){
18
19 if( node == NULL )
20 return 0;
21
22 int res = 0;
23 if( node->val == num )
24 res += 1;
25
26 res += findPath( node->left , num - node->val );
27 res += findPath( node->right , num - node->val );
28
29 return res;
30
31 }
32 };

[刷题] 437 Paths 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. 437. Path Sum III

    原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...

  3. 【leetcode】437. Path Sum III

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

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

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

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

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

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

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

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

  9. LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

    1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...

随机推荐

  1. [GDKOI2021] 普及组 Day3 总结 && 题解

    [ G D K O I 2021 ] 普 及 组 D a y 3 总 结 时间安排和昨天的GDKOI2021 Day2一样. 早上四个小时的快乐码题时间,然鹅我打了半小时的表 然后就是下午的题目讲解和 ...

  2. 201871030103-陈荟茹 实验二 个人项目―《D{0-1}KP问题》项目报告

    项目 内容 课程班级博客链接 班级博客链接 这个作业要求链接 作业要求链接 我的课程学习目标 1.理解掌握软件设计的过程中的各个环节2.掌握github的使用,将自己的项目上传至githu中 这个作业 ...

  3. 一文简述JAVA内部类和异常

    内部类和异常 内部类 在一个类的内部定义的一个类,例如,A类中定义了一个B类,则B类相对于A类就是内部类,而A类相对于B类就是外部类 成员内部类 静态内部类 局部内部类 匿名内部类 成员内部类 pub ...

  4. 02 . MongoDB复制集,分片集,备份与恢复

    复制集 MongoDB复制集RS(ReplicationSet): 基本构成是1主2从的结构,自带互相监控投票机制(Raft(MongoDB)Paxos(mysql MGR 用的是变种)) 如果发生主 ...

  5. Spring Cloud Gateway 全局通用异常处理

    为什么需要全局异常处理 在传统 Spring Boot 应用中, 我们 @ControllerAdvice 来处理全局的异常,进行统一包装返回 // 摘至 spring cloud alibaba c ...

  6. 【CTF】WDCTF-2017 3-1 writeup

    题目来源:WDCTF-2017 题目链接:https://adworld.xctf.org.cn/task/answer?type=misc&number=1&grade=1& ...

  7. shell脚本 5 sed和awk

    文本处理三剑客 在 Shell 下使用这些正则表达式处理文本最多的命令有下面几个工具: 命令 描述 grep 默认不支持扩展表达式,加-E 选项开启 ERE.如果不加-E 使用花括号要加转义符\{\} ...

  8. JDBC_14_使用JDBC工具类实现模糊查询

    使用JDBC工具类实现模糊查询 代码: import java.sql.*; /** * 模糊查询 * 测试DBUtils */ public class JDBCTest09 { public st ...

  9. Insertion Sort and Merge Sort

    Insertion Sort(插入排序) 思路:for 循环遍历数组中的每一个数 用while将每次遍历到的数于左侧的数进行对比,将小的排到左边 void InsertionSort(int*A, i ...

  10. hdu3449 有依赖的背包问题

    题意:       给你一些物品,每个物品有自己的价值和花费,每个物品都对应一个箱子,每个箱子有价钱,买这个物品必须买相应的箱子,给你一个价钱,问最多可以获得多少价值 <提示:多个物品可能同时对 ...