题目描述

给定一个二叉树和一个值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. JavaScript查找字符串中给定字符出现的位置以及次数

    要求: 给定字符串oabcoefoxyozzopp,要求输出字符o出现的位置和次数. 实现思路: 先查找第一个o出现的位置 然后只要判断indexOf返回的结果,若不是-1,则继续往后查找 因为ind ...

  2. 达梦产品技术支持培训-day7-DM8数据库备份与还原-原理

    (本文部分内容摘自DM产品技术支持培训文档,如需要更详细的文档,请查询官方操作手册,谢谢) 1.DM8备份还原简介 1.1.基本概念 (1)表空间与数据文件 ▷ DM8表空间类型: ▷ SYSTEM ...

  3. 如何获取value值,获取属性值,设置属性值,

    1.获取select下拉框的value值,   2.获取select  的tid值 3.设置属性值  4.判断哪个单选框选中了 prop好像是判断的意思吧,个人理解勿喷谢谢!!!!!!

  4. monolog 日志

    1 安装 composer require monolog/monolog 2 使用 // 创建日志服务 $logger = new Logger('my_logger'); // 定义一个handl ...

  5. linux(centos8):安装jmeter5.3

    一,jmeter的用途: Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试, 它最初被设计用于Web应用测试,但后来扩展到其他测试领域.  Apach ...

  6. 查询MS sql server里的所有表 SQL中所有列,说明,类型 查询总存储过程数

    1.查询SQL中的所有表: Select TABLE_NAME FROM   "你的数据库名称".INFORMATION_SCHEMA.TABLES Where TABLE_TYP ...

  7. python 操作conf配置文件方法

    参考文章链接:https://blog.csdn.net/qq_23587541/article/details/85019610

  8. 2020年的UWP(2)——In Process App Service

    最早的时候App Service被定义为一种后台服务,类似于极简版的Windows Service.App Service作为Background Task在宿主UWP APP中运行,向其他UWP A ...

  9. 手把手教你使用 Nginx Ingress 实现金丝雀发布

    概述 本文将介绍如何使用 Nginx Ingress 实现金丝雀发布,从使用场景分析,到用法详解,再到上手实践. 前提条件 集群中需要部署 Nginx Ingress 作为 Ingress Contr ...

  10. eclipse配置打开选中文件存储的目录快捷配置

    方便同时复制多个包的文件 https://jingyan.baidu.com/article/adc8151353a896f723bf73cd.html