Path Sum II
来源:互联网 发布:sqlserver数据恢复 编辑:程序博客网 时间:2024/05/01 20:06
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 a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: void dfs_Path(TreeNode* node, int sum, int curSum, vector<int> path, vector<vector<int> > &pathSet){ if (node == NULL) return; path.push_back(node->val); curSum += node->val; if(node->left == NULL && node->right == NULL){//到达叶节点 if(curSum == sum){//如果符合要求 pathSet.push_back(path);//保存 } return; } dfs_Path(node->left, sum, curSum, path, pathSet); dfs_Path(node->right, sum, curSum, path, pathSet); } vector<vector<int> > pathSum(TreeNode* root, int sum) { vector<vector<int> > pathSet; vector<int> path; dfs_Path(root, sum, 0, path, pathSet); return pathSet; }};
0 0
- LeetCode: Path Sum II
- LeetCode Path Sum II
- [Leetcode] Path Sum II
- LeetCode: Path Sum II
- Path Sum II
- [LeetCode] Path Sum II
- 【leetcode】Path Sum II
- [113]Path Sum II
- Path Sum II
- Path Sum II
- [LeetCode]Path Sum II
- Path Sum II
- Path Sum II
- [Leetcode]Path Sum II
- [leetcode]Path Sum II
- Path Sum II
- Leetcode: Path Sum II
- Path Sum II
- [Sqoop]利用sqoop对mysql执行DML操作
- 【EPI-6.6】最长连续递增子序列-启发式搜索
- Dialog显示在Activity不同的位置
- 隐马尔科夫学习五(四)
- 随机过程的参考书籍
- Path Sum II
- Thinking in Java 第四章、第五章
- 单点登录
- 搜索技术概述
- Python通过suds调用WCF服务 传递Python List 到 WCF Service
- Eclipse常见错误及解决方案
- CentOS7 上源码安装KVM(qemu--kvm)
- ES6新特性概览
- 我的Android系统学习笔记——HAL