leetcode之path-sum-ii(求所有和为固定值的二叉树路径)
题目
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 andsum = 22,
return:
解题思路
这里其实考察的是二叉树的深度遍历,这里采用递归的方式去遍历,然后最终如果还是叶子节点并且sum==给定值时,则返回对应vector,加入vv中。
C++实现代码
/**
* 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> >