[LeetCode 解题报告]113. 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.

Note: A leaf is a node with no children.

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]
]

考察:二叉树路径和等于指定值有哪些; 

/**
 * Definition for a binary tree node.
 * 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> out;
        pathSumDFS(root, sum, out, res);
        return res;
    }
    
    void pathSumDFS(TreeNode* root, int sum, vector<int>& out, vector<vector<int>>& res) {
        if (root == NULL)
            return ;
        out.push_back(root->val);
        if (root->left == NULL && root->right == NULL && root->val == sum)
            res.push_back(out);
        
        pathSumDFS(root->left, sum-root->val, out, res);
        pathSumDFS(root->right, sum-root->val, out, res);
        out.pop_back();
    }
    
    
};
发布了401 篇原创文章 · 获赞 39 · 访问量 45万+

猜你喜欢

转载自blog.csdn.net/caicaiatnbu/article/details/103359366
今日推荐