题目描述

给定一个二叉树和一个值sum,请找出所有的根节点到叶子节点的节点值之和等于sum的路径,
例如:
给出如下的二叉树,sum=22,
    5
    / \
  4  8
  /    / \
11  13 4
/ \      / \
7 2   5 1       
返回
[
    [5,4,11,2],
    [5,8,4,5]
]

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree andsum = 22,

    5
    / \
  4  8
  /    / \
11  13 4
/ \      / \
7 2   5 1    

return
[
    [5,4,11,2],
    [5,8,4,5]
]

示例1

输入

复制

{1,2},1

输出

复制

[]
示例2

输入

复制

{1,2},3

输出

复制

[[1,2]]

/**
 * struct TreeNode {
 *    int val;
 *    struct TreeNode *left;
 *    struct TreeNode *right;
 * };
 */

class Solution {
public:
    /**
     *
     * @param root TreeNode类
     * @param sum int整型
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > pathSum(TreeNode* root, int sum) {
        // write code here
        vector <vector <int>> vv;
        vector <int> v;
        pathSum_Aux(root,sum,v,vv);
        return vv;
    }
    void pathSum_Aux(TreeNode *root,int sum,vector <int> v,vector<vector<int>>&vv){
        if (root==NULL)
            return ;
        v.push_back(root->val);
        if (root->left ==NULL && root->right==NULL &&sum -root->val==0){
            vv.push_back(v);
        }
        pathSum_Aux(root->left, sum-root->val, v,vv);
        pathSum_Aux(root->right,sum-root->val,v,vv);
        
    }
};
/**
 * struct TreeNode {
 *    int val;
 *    struct TreeNode *left;
 *    struct TreeNode *right;
 * };
 */

class Solution {
    vector <vector<int>>res;
    void dfspath(TreeNode *root,int num,vector<int>v){
        if (!root)return ;
        v.push_back(root->val);
        if (root->left ==nullptr && root->right==nullptr){
            if (num==root->val )res.push_back(v);
            
        }
        dfspath(root->left,num-root->val,v);
        dfspath(root->right,num-root->val,v);
    }
public:
    /**
     *
     * @param root TreeNode类
     * @param sum int整型
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > pathSum(TreeNode* root, int sum) {
        // write code here
        vector <int>v;
        dfspath(root, sum, v);
        return res;
        
    }
};
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

#
#
# @param root TreeNode类
# @param sum int整型
# @return int整型二维数组
#
class Solution:
    def pathSum(self , root , sum ):
        # write code here
        if not root:
            return []
        res=[]
        
        def helper(root,remain,temp=[]):
            temp_=temp+[root.val]
            remain_=remain-root.val
            if remain_==0 and not root.left and not root.right:
                res.append(temp_)
                return
            if root.left:
                helper(root.left,remain_,temp_)
            if root.right:
                helper(root.right,remain_,temp_)
        helper(root,sum)
        return res

leetcode37:path-sum-ii的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  3. [leetcode]Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  4. 【leetcode】Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  5. 32. Path Sum && Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

  6. LeetCode: Path Sum II 解题报告

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  7. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  8. Path Sum,Path Sum II

    Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...

  9. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

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

随机推荐

  1. Go 接口类型

    接口作用 Go语言中的接口是一种类型,类似于Python中的抽象基类. Go语言中使用接口来体现多态,是duck-type的一种体现. 如,只要一个东西会叫,会走,那么我们就可以将它定义为一个动物的接 ...

  2. STM32之旅4——USART

    STM32之旅4--USART 串口也是用的比较多的,在STM32CubeMX中生成代码后,需要添加一些代码才可以用. drv_usart.h: #ifndef __DRV_USART_H #defi ...

  3. Python中字符串有哪些常用操作?纯干货超详细

  4. HanLP的分词统计

    HanLP的分词效果鄙人研究了HanLP,他的分词效果确实还可以,而且速度也比较快,10的数据是9000毫秒 @SneakyThrows@Overridepublic LinkedHashMap< ...

  5. MeteoInfoLab脚本示例:获取气团轨迹每个节点的气象数据

    读取HYSPLIT输出的轨迹数据文件和相应时间的气象数据文件,生成轨迹图层,循环每条轨迹的节点,读出该节点的经度.纬度.气压.时间,通过对气象数据插值获得该节点的气象数据.脚本程序: #------- ...

  6. 【树形DP】CF 1293E Xenon's Attack on the Gangs

    题目大意 vjudge链接 给n个结点,n-1条无向边.即一棵树. 我们需要给这n-1条边赋上0~ n-2不重复的值. mex(u,v)表示从结点u到结点v经过的边权值中没有出现的最小非负整数. 计算 ...

  7. vagrantfile-参考示例

    Vagrantfile 文件  bt为你需要新建的box名字    Vagrant.configure("2") do |config|   config.vm.box = &qu ...

  8. 赋予楼宇“智慧大脑”:厦门双子塔3D可视化

    前言 今年10月7日,是国务院批准设立厦门经济特区40周年纪念日.1980年的这一天,国务院正式批复同意在厦门湖里地区划出一块2.5平方公里的土地,设立经济特区.厦门,成为中国最早设立的四个经济特区之 ...

  9. hbase的Java基本操作

    hbase的Java基本操作 建表,建列簇操作 private static Connection connection; private static Admin admin; public sta ...

  10. Linux 打开core dump功能

    系统打开core dump功能 在终端中输入命令 ulimit -c ,输出的结果为 0,说明默认是关闭 core dump 的,即当程序异常终止时,也不会生成 core dump 文件: 使用命令  ...