**(leetcode) (tree) Path Sum II
来源:互联网 发布:男人越大越好吗知乎 编辑:程序博客网 时间:2024/05/22 06:32
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]]
思路: 每条路径都给一个vector存储即可
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution { void path(TreeNode *root, vector<vector<int> > &result, vector<int> v, int sum){ if(root==NULL) return; v.push_back(root->val); vector<int> v1(v); sum-=root->val; if(sum==0&&root->left==NULL&&root->right==NULL) result.push_back(v); else{ path(root->left, result, v, sum); path(root->right, result, v1, sum); } }public: vector<vector<int> > pathSum(TreeNode *root, int sum) { vector<vector<int> > result; vector<int> v; path(root, result, v, sum); return result; }};
0 0
- **(leetcode) (tree) Path Sum II
- LeetCode[Tree]: Path Sum II
- [leetcode][tree][dfs] 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
- Android下载管理DownloadManager功能扩展和bug修改
- 自动开启GPS服务
- SourceInsight使用技巧
- Java GC系列(1):Java垃圾回收简介
- Bash Shell - 使用内置的set和shopt命令设置bash选项
- **(leetcode) (tree) Path Sum II
- Android自动滚动 轮播循环的ViewPager
- 第十三周项目1:数组大折腾(1)
- Linux内存管理之mmap详解
- Linux touch 使用详解
- vc2010 一运行整个项目查找就卡死情况!
- hbase filter练习
- 深度探索C++对象模型
- Spring MVC 学习笔记11 —— 后端返回json格式数据