leetcode 113 path Sum II 路径和

递归先序遍历+vector<int>容器记录路径
 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), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<vector<int>> pathSum(TreeNode* root, int sum) {
13         vector<int> out;
14         vector<vector<int>> res;
15         dfs(root,sum,0,out,res);
16         return res;
17     }
18     //还是递归先序遍历+vector<int>容器记录路径
19     void dfs(TreeNode* node,int sum,int curSum,vector<int> &out,vector<vector<int>> &res){
20         if(!node) return;
21         curSum+=node->val;
22         out.push_back(node->val);
23         if(node->left==NULL&&node->right==NULL&&curSum==sum) res.push_back(out);
24         dfs(node->left,sum,curSum,out,res);
25         dfs(node->right,sum,curSum,out,res);
26         out.pop_back();
27     }
28 };

原文地址:https://www.cnblogs.com/joelwang/p/10453683.html

时间: 2024-11-05 12:17:18

leetcode 113 path Sum II 路径和的相关文章

LeetCode 113. Path Sum II路径总和 II (C++)

题目: 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

LeetCode 113. Path Sum II 20170705 部分之前做了没写的题目

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] ] 题目大意:给定一棵二叉树和一个总和,找出所有从根节点

113 Path Sum II 路径总和 II

给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22,              5             / \            4   8           /   / \          11  13  4         /  \    / \        7    2  5   1返回[   [5,4,11,2],   [5,8,4,5]] 详见:https://leetcode.com/problems/path

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. 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] ] Subscribe to see which co

leetcode 113 Path Sum II ----- java

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] ] 上一道题的延伸,求出所有结果. /** * Defi

Java for 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. 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,JAVA实现如下: stati

【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】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 不同

[LeetCode][JavaScript]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] ] https://leetco