题目描述

给定一个二叉树和一个值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. DX12龙书 02 - DirectXMath 库中与向量有关的类和函数

    0x00 需要用到的头文件 #include <DirectXMath> #include <DirectXPackedVector.h> using namespace Di ...

  2. JavaScript高级程序设计(第4版)pdf 电子书

    JavaScript高级程序设计(第4版)pdf 电子书 免责声明:JavaScript高级程序设计(第4版)pdf 电子书下载 高清收集于网络,请勿商用,仅供个人学习使用,请尊重版权,购买正版书籍. ...

  3. 解决SpringBoot 定时计划 quartz job 任务重复执行多次(10次)

    上一篇:SpringBoot多任务Quartz动态管理Scheduler,时间配置,页面+源 设置了多个 任务,本应该是各司其职的,任务调用多线程处理任务,but这个定时任务竟然同时跑了10次???如 ...

  4. Python数据类型--字典(dict)

    Python中的字典是键值对(key-value)的无序集合.每个元素包含"键"和"值"两部分,这两部分之间使用冒号分隔,表示一种对应关系.不同元素之间用逗号分 ...

  5. SQL Server语法入门

    1.说明:增加.删除一个列 Alter table tablename add columnName col type alter table tablename drop columnName co ...

  6. ansible用authorized_key模块批量推送密钥到受控主机(免密登录)(ansible2.9.5)

    一,ansible的authorized_key模块的用途 用来配置密钥实现免密登录: ansible所在的主控机生成密钥后,如何把公钥上传到受控端? 当然可以用ssh-copy-id命令逐台手动处理 ...

  7. python爬取知乎评论

    点击评论,出现异步加载的请求 import json import requests from lxml import etree from time import sleep url = " ...

  8. requests设置代理ip

    # coding=utf-8 import requests url = "http://test.yeves.cn/test_header.php" headers = { &q ...

  9. 选择SaaS平台的那些事

    将近一年多没有更新博客和自己的订阅号.除了本身有点懒之外,也有幸在上半年花了一些时间考出了CISSP.最近也在研究云平台相关的一些课题. 写这篇文章本身是因为在工作中经常有IT乃至业务的同事会问及企业 ...

  10. day70:Vue:Git&路飞学城页面效果

    目录 1.Git 2.路飞学城项目页面效果 0.安装elements UI 1.顶部导航栏效果 2.轮播图效果 1.Git 什么是git?分布式版本管理工具 1.git操作 # 1 创建git本地仓库 ...