码迷,mamicode.com
首页 > 其他好文 > 详细

[Leetcode Week14]Path Sum II

时间:2017-12-06 23:54:04      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:turn   source   http   blog   leetcode   public   problems   nod   desc   

Path Sum II 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/path-sum-ii/description/


Description

Given a binary tree and a sum, find all root-to-leaf paths where each path‘s sum equals the given sum.

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

Solution

class Solution {
    void dfs(vector<vector<int>>& res, vector<int>& path, TreeNode* root, int sum) {
        if (root -> left == NULL && root -> right == NULL) {
            if (sum == root -> val) {
                path.push_back(root -> val);
                res.push_back(path);
                path.pop_back();
            }
            return;
        }
        if (root -> left != NULL) {
            path.push_back(root -> val);
            dfs(res, path, root -> left, sum - (root -> val));
            path.pop_back();
        }
        if (root -> right != NULL) {
            path.push_back(root -> val);
            dfs(res, path, root -> right, sum - (root -> val));
            path.pop_back();
        }
    }
public:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>> res;
        if (root == NULL)
            return res;
        vector<int> path;
        dfs(res, path, root, sum);
        return res;
    }
};

解题描述

这道题目题意是,在一棵二叉树中,寻找一个从根节点到叶子节点的路径,使得路径上的数字之和为给定的数字sum,要求找出所有这样的路径。当然最容易想到的就是使用DFS来解决。

[Leetcode Week14]Path Sum II

标签:turn   source   http   blog   leetcode   public   problems   nod   desc   

原文地址:http://www.cnblogs.com/yanhewu/p/7995213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!