题目描述

给定一个二叉树和一个值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. docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled

    故障描述: [root@docker01 ~]# docker run centos docker: Error response from daemon: Get https://registry- ...

  2. javascript 数据类型判断总结

    一 typeof 回顾:js有五种基本数据类型:值类型("number","string","boolean","undefine ...

  3. 多测师讲解接口测试 _postman(下)_高级讲师肖sir

    关联接口 定义:上个接口返回的参数作为下一个接口的入参 1)接口1:查询出所有的州,自治区,直辖市,省(且发送请求不需要入参) 接口url地址: http://www.webxml.com.cn/We ...

  4. 【Jenkins】远程调用jenkins进行构建方式!

    前提:jenkins支持远程调用(具体设置自行百度)1.在我的个人中心--configure--API TOKEN--如果没有,则添加一个token,并生成,再复制并记录下来2.在你的job上面加上你 ...

  5. day38 Pyhton 并发编程

    # 网络编程 # arp协议 : # 1.这是一个通过ip找mac地址的协议 # 2.由于有了socket,用户在使用网络的时候,只需要关心对方用户的ip地址就可以了 # 3.如果用户即将和这个ip进 ...

  6. pytest文档56-插件打包上传到 pypi 库

    前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...

  7. selenium常用操作学习笔记

    一,弹窗处理(推荐文章:https://blog.csdn.net/huilan_same/article/details/52298460) selenium提供switch_to方法定位弹窗的对话 ...

  8. 【Linux教程】Linux系统零基础编程入门,想当大神?这些你都要学

    ✍ 文件和文件系统 文件是Linux系统中最重要的抽象,大多数情况下你可以把linux系统中的任何东西都理解为文件,很多的交互操作其实都是通过文件的读写来实现的. 文件描述符 在Linux内核中,文件 ...

  9. centos8平台使用strace跟踪系统调用

    一,strace的用途 strace  是最常用的跟踪进程系统调用的工具. 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectfore ...

  10. flink 处理实时数据的三重保障

    flink 处理实时数据的三重保障 window+watermark 来处理乱序数据对于 TumblingEventTimeWindows window 的元数据startTime,endTime 和 ...