Path Sum II
来源:互联网 发布:g71格式编程 编辑:程序博客网 时间:2024/06/05 14:55
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]]
ret
/** * 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 pathArray(TreeNode *root,int sum,vector<vector<int> > &vec,vector<int> subVec) { if(root){ sum-=root->val; subVec.push_back(root->val); }else{ return; } if(root->left==nullptr&&root->right==nullptr){ if(sum==0){ vec.push_back(subVec); } } pathArray(root->left,sum,vec,subVec); pathArray(root->right,sum,vec,subVec); } vector<vector<int> > pathSum(TreeNode *root, int sum) { vector<vector<int> > vec; vector<int> subVec; pathArray(root,sum,vec,subVec); return vec; }};
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
- scrollNumber 滚动视图 随机
- springmvc文件上传和拦截器
- Ubuntu安装配置Qt环境
- Access to the path '/Files/Images/' is denied. 如何处理此类问题
- Fixing a Mac OSX Leopard Login Loop Caused by Launch Services
- Path Sum II
- [LeetCode] Regular Expression Matching [6]
- 64位win7 下Eclipse 安装svn插件 及问题解决
- 去重复数据(JAVA)
- VC++截取输入法输入信息钩子dll实现
- 如何在网页中嵌入百度地图
- 下载xcode 6 beta.dmg
- The Building Blocks-Components of EA part 1- Information and Strategy
- [iOS] UITextField隐藏软键盘心得(隐藏自身软键盘、点击Return自动转到下个文本框、轻触背景隐藏软键盘)