113. Path Sum II
来源:互联网 发布:贴吧防秒删软件 编辑:程序博客网 时间:2024/05/02 05:00
使用递归的方法,传递path变量,sum减去经过path经过节点的值,最后为0,且为根节点即可。
class Solution {public: vector<vector<int>> pathSum(TreeNode* root, int sum) { vector<int> path; vector<vector<int>> allPath; findPath(root, sum, path, allPath); return allPath; } void findPath(TreeNode* root, int sum, vector<int> &path, vector<vector<int>> &allPath) { if(!root) return; sum -= root->val; path.push_back(root->val); if(!root->left && !root->right) { if(sum == 0) allPath.push_back(path); } else { if(root->left) findPath(root->left, sum, path, allPath); if(root->right) findPath(root->right, sum, path, allPath); } path.pop_back(); }};
0 0
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- QC安装验证错误
- Java中校驗IPv4和IPv6
- Qt插件问题
- 【故障处理】告警日志报“ORA-01565 Unable To open Spfile”
- 浅谈JVM的实现与垃圾回收机制
- 113. Path Sum II
- Android 6.0动态权限申请--事例代码
- LPC中Fixed linear predictor的详细说明
- 2016年 中国大学生程序设计竞赛合肥赛区(ccpc)解题报告
- Maven 安装与Eclipse搭建
- intellij idea 初识,Hello Idea!!
- (文章非原创)solr集群搭建和 java调用 solr集群
- C++学习第二课笔记
- HTML5--服务器发送事件