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 ...
随机推荐
- spring-boot-route(十二)整合redis做为缓存
redis简介 redis作为一种非关系型数据库,读写非常快,应用十分广泛,它采用key-value的形式存储数据,value常用的五大数据类型有string(字符串),list(链表),set(集合 ...
- 跨境 TCP 传输优化实录 — 使用 BBR 解决 LFN 问题
背景 近期开通了一条访问美国机房的 1G 专线,用于提供行情数据备源,并基于 TCP 建立了一套数据传输服务.上线后发现一个严重的问题:应用程序发送队列中的数据大量积压,最终导致程序 OOM Kill ...
- MeteoInfoLab脚本示例:计算不同区域平均值
这里用美国做例子,有一个美国区域的格点温度场数据(usgrid.data),需要计算出每个州(state)的平均温度.当然需要有一个包含各州行政区域的shape文件了(相关文件可以在此帖中下载:htt ...
- day23 Pyhton学习 昨日回顾.re模块.序列化模块
一.昨日回顾 #__file__查看当前文件所在的绝对路径 #time 时间模块 time.time 获取当前时间戳时间 字符串->time.strptime->结构化->mktim ...
- BUUCTF-misc九连环 详解
这个没什么玄学,我们解压出来一张照片,放到hxd中搂一眼,最后结尾的那几行中看到了zip压缩包的结尾标识符,难道这里面还有压缩包,于是我们就formostlrb 果然有图片有压缩包 我们打开压缩包看到 ...
- linux学习(一)--启动文件bootsect.s
这是linux由BIOS加载后执行的第一段的启动程序代码,即文件 boot/bootsect.s 首先附图,简单介绍一下从开机加电到第一段linux代码执行的简要过程 1 .globl begte ...
- gorm学习地址
1 gorm curd指南 2 gorm入门指南
- linux(centos8):sed命令的应用例子
一,sed命令的用途 sed是Linux下一款功能强大的非交互流式文本编辑器, 可以对文本文件进行增.删.改.查等操作, 支持按行.按字段.按正则匹配文本内容. 说明:刘宏缔的架构森林是一个专注架构的 ...
- JS获取DropDownList选择项的值
var dropDownList= document.getElementById("<%=DropDownListID.ClientID %>");//获取DropD ...
- C++ 多线程 std::thread 使用总结
在C++ 11之前,官方并没有支持线程库.C++ 11通过标准库引入了对 thread 类的支持,大大方便了完成多线程开发的工作. std::thread 构造函数 (1)thread() noex ...