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 ...
随机推荐
- ie 版本判断脚本
// 获取IE版本 /** * @return {string} */ function IEVersion() { // 取得浏览器的userAgent字符串 var userAgent = nav ...
- CDH5部署三部曲之二:部署和设置
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- vs code个性化设置
文件=>首选项=>设置,直接在搜索栏搜索有背景色的部分即可 1. 鼠标滚轮缩放 "editor.mouseWheelZoom": true 2. 显示空格和tab符号 ...
- 【树形DP】BZOJ 3829 Farmcraft
题目内容 mhy住在一棵有n个点的树的1号结点上,每个结点上都有一个妹子i. mhy从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装zhx牌杀毒软件,第i个妹子安装时间为Ci. ...
- k8s-命令创建service
查看命令帮助 [root@master kubernetes]# kubectl create service -h Create a service using specified subcomma ...
- CentOS安装MySQL详解 转
引言 最近某云搞活动,买了个服务器作为平时学习和测试用,新机器啥也没有,一些常用软件的安装是免不了的,于是乎想着把安装过程都详细记录下来,一是做个备忘,二是给有需要的同学作个参考. Linux上安 ...
- C#Socket通讯(1)
前言 因为自己需要开发一款自己的游戏,因为设计网络方面,所以我找了很多的资料,再加上考虑到Unity游戏客户端直接连接数据库很容易导致数据库泄露,再加上网上很多的服务端都是用控制台做的,所以我就想做个 ...
- CC2530定时器模模式最大值计算
首先假设 频率: f 分频系数: n 间隔定时: s 周期: T 模模式最大值: N 因为 T = 1 / f 所以 s = ( n / f ) * N = n * N / f 由此可得 计算模模 ...
- pyqt设置窗口图标
import sys from PyQt5.QtWidgets import QMainWindow,QApplication from PyQt5.QtGui import QIcon ''' 窗口 ...
- ECC ~ Edge-Conditioned Filter in CNN on Graphs
ECC的卷积操作和常规的二维图像卷积操作都是一种加权平均操作,不同之处在于ECC可以作用在任何图结构上,并且其权重由节点间的边权所决定. 考虑$G=(V,E)$, 其中$|V|=n$ 边 $E \in ...