Path Sum II深度优先找路径
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 and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
vector<vector<int> > paths;
public:
void dfs(TreeNode *node,int sum,int csum,vector<int> onePath){ //a能为引用
if(node==NULL)
return;
if(node->left==NULL && node->right==NULL){
if(node->val+csum==sum){
onePath.push_back(node->val);
paths.push_back(onePath);
}
return;
}
onePath.push_back(node->val);
dfs(node->left,sum,csum+node->val,onePath);
dfs(node->right,sum,csum+node->val,onePath);
} vector<vector<int> > pathSum(TreeNode *root, int sum) {
paths.clear();
vector<int> onePath;
dfs(root,sum,,onePath);
return paths;
}
};
Path Sum II深度优先找路径的更多相关文章
- [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
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Path Sum II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [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 ...
随机推荐
- 我的js运动库新
1.一些样式的获取和设置 //通过id获取当前元素 //params:id function $id(id) { return document.getElementById(id); } //向cs ...
- mysql查看执行计划重构后的查询
MYSQL优化器会对客服端发送的SQL语句进行优化,优化后的SQL语句再被MYSQL执行.然后我们在优化SQL的时候,怎么获取到MYSQL优化后执行语句呢. EXPLAIN select * from ...
- Django项目:CRM(客户关系管理系统)--66--56PerfectCRM实现CRM客户报名缴费链接
# kingadmin.py # ————————04PerfectCRM实现King_admin注册功能———————— from crm import models #print("ki ...
- ArcGIS Server 10.1安装、配置、发布地图服务
先跟大家分享一个esri的学习资料,http://pan.baidu.com/s/1nBzxB,<ArcGIS10.1 for Server 入门教程>.教程讲述的很清楚,下面说说我这次发 ...
- 原生js 判断变量是一个数组
const arr = [] // 1. 最简单 ES5+ Array.isArray(arr) // 2. 兼容性好的方法,也很准确 Object.prototype.toString.call(a ...
- vue实现跳转路由
参考vue官方文档:https://router.vuejs.org/zh/guide/essentials/navigation.html // 字符串 router.push('home') // ...
- 关闭浏览器或者关闭使用window.open打开的页面时添加监听事件
最近工作中有个需求:点击按钮时打开一个页面,此处取名为page1,打开页面的前提条件是如果有人已经打开过page1页面并且没有关闭时请求ajax判断session是否为空,如果为空则将用户名和文档id ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- tesseract3.0.2font_id >= 0 && font_id < font_id_map_.SparseSize():Error:Assert failed:in file ..\..\classify\trainingsampleset.cpp, line 622
https://stackoverflow.com/questions/14025965/mftraining-gives-warning-no-protos-configs-for-f-in-cre ...
- python基础--常用的模块(collections、time、datetime、random、os、sys、json、pickle)
collection模块: namedtuple:它是一个函数,是用来创建一个自定义的tuple对象的,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素.所以我们就可以 ...