LeetCode: Path Sum II [113]

【题目】

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

【题意】

判断二叉树中是否存在一条从根到叶子节点的路径,使得路径上的节点值之和等于所给的值,输出所有的路径

【思路】

DFS,递归

【代码】

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    void dfs(TreeNode*root, vector<vector<int> >&result, vector<int>path, int pathSum, int sum){
        path.push_back(root->val);
        pathSum+=root->val;
        if(root->left==NULL && root->right==NULL){
            if(pathSum==sum) result.push_back(path);
            return;
        }

        //搜索左子树
        if(root->left)
            dfs(root->left, result, path, pathSum, sum);
        //搜索右子树
        if(root->right)
            dfs(root->right, result, path, pathSum, sum);
    }

    vector<vector<int> > pathSum(TreeNode *root, int sum) {
        vector<vector<int> >result;
        if(root==NULL)return result;

        vector<int>path;
        int pathSum=0;
        dfs(root, result, path, pathSum, sum);
        return result;
    }
};

LeetCode: Path Sum II [113],布布扣,bubuko.com

时间: 2024-10-12 08:02:25

LeetCode: Path Sum II [113]的相关文章

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 and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于

LeetCode: Path Sum II 解题报告

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 and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] SOLUTION 1: 使用

[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, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 题意:给定一数,在树中找出所有路径和等于该数的情况.方

[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 and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 分析: 这道题目算是Path Sum的延伸,只不过

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. 通过一个p指针,遍历 二叉树,并将每次的值 保存在 sum2 中 . 遇到右节点,将右节点+depth 保存在 temp中,当再次使用 该节点时,根据depth 将sum2中的长度削减成 depth-1 /** * Definition for a binary tree node. * st

[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 and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] Hide Tags Tree Depth-first

LeetCode Path Sum II (DFS)

题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL)

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium 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

[LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Pr