Leetcode 113
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
if(root == NULL) return res;
vector<int> add;
add.push_back(root->val);
DFS(res,add,root,sum,root->val);
return res; }
void DFS(vector<vector<int>>& res,vector<int>& add,TreeNode*root,int sum,int& he){
if((root->left == NULL)&&(root->right == NULL)){
if(he == sum) res.push_back(add);
}
else if((root->left != NULL)&&(root->right == NULL)){
add.push_back(root->left->val);
he += root->left->val;
DFS(res,add,root->left,sum,he);
add.pop_back();
he -= root->left->val;
}
else if((root->left == NULL)&&(root->right != NULL)){
add.push_back(root->right->val);
he += root->right->val;
DFS(res,add,root->right,sum,he);
add.pop_back();
he -= root->right->val;
}
else if((root->left != NULL)&&(root->right != NULL)){
add.push_back(root->left->val);
he += root->left->val;
DFS(res,add,root->left,sum,he);
add.pop_back();
he -= root->left->val;
add.push_back(root->right->val);
he += root->right->val;
DFS(res,add,root->right,sum,he);
add.pop_back();
he -= root->right->val;
}
return;
}
};
_虽然代码丑,但比较好理解
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int>> res;
vector<int> out;
helper(root, sum, out, res);
return res;
}
void helper(TreeNode* node, int sum, vector<int>& out, vector<vector<int>>& res) {
if (!node) return;
out.push_back(node->val);
if (sum == node->val && !node->left && !node->right) {
res.push_back(out);
}
helper(node->left, sum - node->val, out, res);
helper(node->right, sum - node->val, out, res);
out.pop_back();
}
};
——这个和上一题对应
Leetcode 113的更多相关文章
- [LeetCode] 113. Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Java实现 LeetCode 113 路径总和 II
113. 路径总和 II 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = ...
- Leetcode 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- leetcode 113 Path Sum II ----- java
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [leetcode]113. Path Sum II路径和(返回路径)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Java for LeetCode 113 Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- leetcode 113. Path Sum II (路径和) 解题思路和方法
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- ACM题目————STL练习之求次数
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个 ...
- android 接受系统锁屏广播,及高版本发送广播
protected BroadcastReceiver messageReceiver = new BroadcastReceiver() { @Override public void onRece ...
- j2ee分布式缓存同步实现方案dlcache v1.0.1
j2ee分布式缓存同步实现方案dlcache v1.0.1 发布 修复问题: 1.支持两个层次的缓存,典型的用于产品大类.产品小类,数据字典以及子项: 更新后见: pan http://pan.bai ...
- 04: 使用BeautifulSoup封装的xss过滤模块
目录: 1.1 xss攻击简介 1.2 xss攻击解决方法 1.1 xss攻击简介返回顶部 1.简介 1. 跨站脚本(cross site script)为了避免与样式css混淆,所以简称为XSS. ...
- 微信小程序——3、逻辑js文件
逻辑层js文件 微信小程序前端进行了层次划分,分为逻辑层和视图层.逻辑层实现对数据的加工和处理.与HTML页面相似,逻辑层使用JavaScript编写.逻辑层将数据处理后发送至视图层,同时接受视图层的 ...
- MVC5 一套Action的登录控制流程
流程: 用拦截器控制每一个页面请求和ajax请求,根据请求体的cookie里面是否有token判断是否登录,还必须判断该token在redis里面的缓存是否存在 组成部分: 拦截器: using Sy ...
- Oracle和MySQL的对比
一.概述 1.1 Oracle 1.1.1 优点 开放性:Oracle 能所有主流平台上运行(包括 windows)完全支持所有工业标准采用完全开放策略使客户选择适合解决方案对开发商全力支持: 可伸缩 ...
- 51nod 1043 幸运号码(数位dp
1043 幸运号码 1个长度为2N的数,如果左边N个数的和 = 右边N个数的和,那么就是一个幸运号码. 例如:99.1230.123312是幸运号码. 给出一个N,求长度为2N的幸运号码的数量 ...
- 51nod 1106 质数检测
#include <bits/stdc++.h> using namespace std; int n; ; bool s[maxn]; void is_prime() { memset( ...
- Atcoder Educational DP Contest
前面简单一点的题直接过吧. A 暴力DP B 怎么还是暴力DP C 还是暴力DP D 直接背包 E 这个背包不太一样了,这里有一个技巧,就是因为价值很小,所以直接对价值背包,求出来达到某一个权值最小的 ...