[leetcode] Path Sum II
来源:互联网 发布:天猫数据在线直播 编辑:程序博客网 时间:2024/06/04 20:44
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<vector<int> > pathSum(TreeNode *root, int sum) { vector<vector<int>> res; vector<int> resRow; findPath(res,resRow,root,sum); return res; } void findPath(vector<vector<int>>&res,vector<int>&resRow,TreeNode *root,int sum){ if(root==NULL){ return; } resRow.push_back(root->val); if(root->left==NULL&&root->right==NULL){ if(sum==root->val){ res.push_back(resRow);//在vector尾部添加 } }else{ findPath(res,resRow,root->left,sum-root->val); findPath(res,resRow,root->right,sum-root->val); } resRow.pop_back();//<span style="color: rgb(51, 51, 51); font-family: arial; font-size: 13px; line-height: 20.020000457763672px;">删除当前vector最末的一个元素</span> }};
0 0
- LeetCode: Path Sum II
- LeetCode Path Sum II
- [Leetcode] Path Sum II
- LeetCode: Path Sum II
- [LeetCode] Path Sum II
- 【leetcode】Path Sum II
- [LeetCode]Path Sum II
- [Leetcode]Path Sum II
- [leetcode]Path Sum II
- Leetcode: Path Sum II
- LeetCode-Path Sum II
- [leetcode] Path Sum II
- LeetCode - Path Sum II
- [LeetCode] Path Sum II
- Leetcode Path Sum II
- LeetCode | Path Sum II
- 【leetcode】Path Sum II
- [LeetCode] Path Sum II
- TDF8599C特性、参数介绍及TDF8599C样片申请指南
- [ubuntu] setup chrome
- apache与tomcat与weblogic对比
- HDOJ1175 连连看
- Linux网络编程-若干网络基本知识
- [leetcode] Path Sum II
- BFS
- POJ3304-Segments
- Linux网络编程-UDP
- 第二章 构架属于自己的WMTS服务,数据下载整合篇1
- SharedPreferences详解(三)——存取图片
- Android学习小Demo(21)ListView的联动选择
- 开始程序猿之路
- 树莓派学习笔记——交叉编译练习之SQLite3安装