leetcode_c++:树: Path SumII(113)
来源:互联网 发布:谁人知歌词 编辑:程序博客网 时间:2024/05/18 00:44
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]]
算法
dfs
O(n)
class Solution {private: void dfs(TreeNode *root, int rest, vector<int> &path, vector<vector<int> > &res) { if (root == NULL) return; path.push_back(root->val); rest -= root->val; if (root->left == NULL && root->right == NULL) { if (rest == 0) res.push_back(path); } else { if (root->left) dfs(root->left, rest, path, res); if (root->right) dfs(root->right, rest, path, res); } path.pop_back(); }public: vector<vector<int> > pathSum(TreeNode *root, int sum) { vector<vector<int> > res; vector<int> path; dfs(root, sum, path, res); return res; }};
0 0
- leetcode_c++:树: Path SumII(113)
- LeetCode 113 Path SumII
- path sumII
- leetcode_c++:树: Path Sum(112)
- Leetcode dfs Path SumII
- Path SumII|leetcode题解
- leetcode_c++:栈:Simplify Path(071)
- leetcode_c++:树:Binary Tree Maximum Path Sum(124)
- leetcode_c++:Minimum Path Sum (064)
- leetcode_c++:树:Balanced Binary Tree(110)
- leetcode_c++:树:Binary Tree Paths(257)
- leetcode_c++:树:House Robber III(337)
- leetcode_c++:Merge_Intervals(056)
- Leetcode_c++: Subsets (078)
- leetcode_c++:树: Minimum Depth of Binary Tree(111)
- leetcode_c++:树: Flatten Binary Tree to Linked List(114)
- leetcode_c++:树:Serialize and Deserialize Binary Tree(297)
- Combination SumII
- 最长公共子串(1)--hdu1238(多个字符串得最长公共子串--暴力)
- 中文乱码问题
- InputStream 源码阅读
- Java的接口和抽象类
- js数组操作
- leetcode_c++:树: Path SumII(113)
- Ubuntu 14.04 for Caffe 环境搭建
- 灰色关联度分析
- Spring Boot:在Eclipse/STS设置热插拔免重启
- 深入理解G1垃圾收集器
- Android一面而就最全面试题汇总
- cocos creator第一个星星游戏
- DataTable和list转换
- python 中字典打印中文问题