2017.10.29 LeetCode
来源:互联网 发布:靠网络如何挣钱 编辑:程序博客网 时间:2024/06/03 21:44
附:由于昨天去外地参加比赛,耽误了,补上~
113. Path Sum II
Description
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,
题意: 在一棵二叉树上寻找是否存在从根节点到叶子节点的路径使得权值和为目标值,并存在二维数组里
分析: 引入一个path数组来记录下路径,如果找到头符合目标的话,记录下,这里运用到了vector中std::vector.pop_back的运用,删除最后一个元素,详细看代码
参考函数
class Solution {public: void dfs(TreeNode* root,vector<vector<int> >& res,vector<int>& path,int sum) { if(!root) return; path.push_back(root->val); if(!root->left && !root->right && sum == root->val) { res.push_back(path); path.pop_back();//注意 return; } dfs(root->left,res,path,sum-root->val); dfs(root->right,res,path,sum-root->val); path.pop_back(); } vector<vector<int>> pathSum(TreeNode* root, int sum) { vector<vector<int> > res; vector<int> path; dfs(root,res,path,sum); return res; }};
阅读全文
0 0
- 2017.10.29 LeetCode
- leetcode 29
- 2017.10.21 LeetCode
- 2017.10.22 LeetCode
- 2017.10.23 LeetCode
- 2017.10.25 LeetCode
- 2017.10.27 LeetCode
- 2017.10.28 LeetCode
- 2017.10.30 LeetCode
- 2017.10.31 LeetCode
- [LeetCode] Distinct Subsequences [29]
- leetCode(29):Happy Number
- leetcode #29 in cpp
- leetCode练习(29)
- LeetCode刷题(29)
- leetcode
- [leetcode]
- LeetCode
- 负载均衡----文件服务策略
- 基于flowplayer的视频缩略图的视频预览
- 两表联查的JSON取值
- C/C++:fscanf()
- jsoup教程008-从元素抽取属性,文本和HTML
- 2017.10.29 LeetCode
- CV论文笔记(二) Large-scale Video Classification with Convolutional Neural Networks
- Unable to fence NameNode at 问题
- hbase维护操作命令
- Keras Tutorial: Deep Learning in Python
- 机器学习(九):k-means与聚类
- 按钮布局演示
- 关于swift for 循环 遍历数组删除元素的bug问题
- jsoup教程009-处理URLs