[Leetcode] Path Sum II
2014-03-31 01:16
316 查看
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
return
水。
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 { public: void getPath(TreeNode *root, int sum, vector<vector<int> > &res, vector<int> path) { if (root == NULL) { return; } path.push_back(root->val); if (root->left == NULL && root->right == NULL) { if (root->val == sum) { res.push_back(path); } return; } getPath(root->left, sum - root->val, res, path); getPath(root->right, sum - root->val, res, path); } vector<vector<int> > pathSum(TreeNode *root, int sum) { vector<vector<int> > res; vector<int> path; getPath(root, sum, res, path); return res; } };
相关文章推荐
- 求最大公约数和最小公倍数
- 腾讯CodeStar第二季前端突击队腐蚀的画解法步骤笔记
- jsp学习---css基础知识学习,float,position,padding,div,margin
- Commons DbUtils源码阅读三
- Linux rhel Access denied for user ’root’@’localhost’ (using password: YES)
- 64位系统下启用curl_init
- 加速键表的使用
- achartengine 使用注意点
- [原]Java修炼 之 基础篇(二)Java语言构成
- [Android学习笔记]设置Activity方向
- xcode5.1 armv7 armv7s arm64 类型, 区分, 概念等
- Unity支持的模型文件
- [Leetcode] Path Sum
- C++的一些知识点汇总
- 黑马程序员_2_TreeSet集合Jdk1.7小知识
- Mental ray 的建筑渲染Arch&Design材质
- 浅谈泛型编程
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- properties文件中文乱码解决
- Mental ray 渲染器常用设置