Leetcode #113 Path Sum II
2015-08-09 19:10
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
Difficulty:Medium
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] ]
Difficulty:Medium
/** * 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: vector<vector<int> > ans; void path(TreeNode* root, int sum,vector<int>& nums){ if(root==NULL) return; nums.push_back(root->val); vector<int> nums2 = nums; if(sum-root->val==0&&root->left==NULL&&root->right==NULL) { ans.push_back(nums); } path(root->left,sum-root->val,nums); path(root->right,sum-root->val,nums2); } vector<vector<int> > pathSum(TreeNode* root, int sum) { vector<int> nums; path(root,sum,nums); return ans; } };
相关文章推荐
- SDNU 1040.导弹拦截【NOIP1999】【8月9】
- K近邻算法
- matlab:启动后无法调用工具包
- Leetcode: Max Points on a Line
- HDU1429 胜利大逃亡
- poj 2299树状数组求逆序数
- 老版本上的兼容ripple效果以及通用selecter
- Objective-C学习笔记_类的扩展
- poj 1328Radar Installation
- tomcat整合apache
- tomcat整合apache
- Unix环境高级编程:进程控制-线程控制-僵尸进程
- Struts2 入门实例
- 基于Wi-Fi的室内定位在美团总部的实践和应用
- 华为上机 最小长方形 java
- 从WebView到http协议
- 回归研发一线
- Java笔试面试题004
- GlTail.rb : 超 Geek 的可视化日志分析工具
- HDU 5366 【组合数+隔板法】