leetcode37:path-sum-ii
题目描述
例如:
给出如下的二叉树,sum=22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
返回
[
[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,
/ \
4 8
/ / \
11 13 4
/ \ / \
return
[
[5,4,11,2],
[5,8,4,5]
]
输出
[[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的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- 【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 ...
- 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 ...
- 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 ...
- [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 ...
- Path Sum,Path Sum II
Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...
- 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 ...
- 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 ...
随机推荐
- IDEA项目区模块文件变为红色解决办法
解决方法 先检查文件格式是否为.java格式..class格式就不行. 选择file–>setting–>version Controller,然后把vcs选项选择为none
- day22 函数整理
# 1.计算 年月日时分秒 于现在之间差了多少 格式化时间 # 现在 # 某一个年月日时分秒 参数 # import time # def get_time(old_t,fmt = '%Y-%m-%d ...
- spring boot:接口站增加api版本号后的安全增强(spring boot 2.3.3)
一,接口站增加api版本号后需要做安全保障? 1,如果有接口需要登录后才能访问的, 需要用spring security增加授权 2,接口站需要增加api版本号的检验,必须是系统中定义的版本号才能访问 ...
- centos8安装及配置nfs4
一,用rpm检查是否有nfs-utils的包已安装 [root@localhost liuhongdi]# rpm -qa | grep nfs-utils nfs-utils-2.3.3-26.el ...
- 后羿:我射箭了快上—用MotionLayout实现王者荣耀团战
前言 昨晚跟往常一样,饭后开了一局王者荣耀,前中期基本焦灼,到了后期一波决定胜负的时候,我果断射箭,射中对面,配合队友直接秒杀,打赢团战一波推完基地.那叫一个精彩,队友都发出了666666的称赞,我酷 ...
- Helium文档9-WebUI自动化-find_all获取页面table数据
前言 find_all关键字根据官方介绍的作用是查找所有出现GUI元素,并且返回list,下面通过举例说明 入参介绍 def find_all(predicate): ""&quo ...
- Linux命令之{ }花括号
括号扩展:{ } {} 可以实现打印重复字符串的简化形式 [10:04:14 root@C8[ 2020-06-16DIR]#echo file{1,3,5} file1 file3 file5 [1 ...
- 智能DNS的实现
网络路径远,导致用户访问延迟 各个运营商之间的带宽有阀口. GSLB 就近的返回服务器的地址 CDN网络 内容分发网络 Content Delivery Network CND服务商 阿里 腾讯 蓝汛 ...
- 【多次实践】win10+ubuntu18.04lts双系统安装葵花宝典(安装篇)
这个教程诞生的缘由很简单,吃的太饱,硬是要折腾,结果,这一折腾便是20余小时,故写此文,帮助后来者少走弯路! 在本文开始,请先允许我对网上很多类似的教程嗤之以鼻,很成功地让我走了很多的弯路,一些有效简 ...
- JS里的小细节,持续更新
判断把值定为 false 集合 JavaScript里把 null.undefined.0.''.NaN 都视为false,而其他值一概为 true Map Map是一组键值对的结构,具有极快的查找速 ...