Question:
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]
]
Anwser 1:
/**
* 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 calPath(TreeNode *root, int sum, vector<int> tmp, vector< vector<int> > &ret){
if(root == NULL){
return;
}
tmp.push_back(root->val);
if(sum == root->val && root->left == NULL && root->right == NULL){
ret.push_back(tmp);
}
calPath(root->left, sum - root->val, tmp, ret);
calPath(root->right, sum - root->val, tmp, ret);
tmp.pop_back();
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector< vector<int> > ret;
if(root == NULL) {
return ret;
}
vector<int> tmp;
calPath(root, sum, tmp, ret);
return ret;
}
};
Anwser 2:
/**
* 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 calPath(TreeNode *root, int sum, vector<int> tmp, vector< vector<int> > &ret){
if(root == NULL){
return;
}
tmp.push_back(root->val);
if(sum == root->val && root->left == NULL && root->right == NULL){
ret.push_back(tmp);
tmp.pop_back(); // pop tmp vector
return;
}
calPath(root->left, sum - root->val, tmp, ret);
calPath(root->right, sum - root->val, tmp, ret);
tmp.pop_back();
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector< vector<int> > ret;
if(root == NULL) {
return ret;
}
vector<int> tmp;
calPath(root, sum, tmp, ret);
return ret;
}
};
分享到:
相关推荐
java java_leetcode-113-path-sumII
python python_leetcode题解之113_Path_Sum_II
javascript js_leetcode题解之113-path-sum-ii.js
java java_leetcode-112-path-sum
public int PathSum(TreeNode root, int sum) { _count = 0; _sum = sum; Travel(root, new List()); return _count; } private void Travel(TreeNode current, List<int> ret) { if (current == null) { ...
python python_leetcode题解之112_Path_Sum
LeetCode — Path Sum III分析及实现方法 题目描述: You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need...
javascript js_leetcode题解之112-path-sum.js
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...
javascript js_leetcode题解之64-minimum-path-sum.js
python python_leetcode题解之124_Binary_Tree_Maximum_Path_Sum
此外,文档还提到了另一个问题——路径总和II(Path Sum II),这是一个寻找二叉树中特定和的路径的问题。虽然没有给出完整的代码,但可以看出这个问题涉及到递归或深度优先搜索(DFS)的策略,可能需要使用回溯法来...
371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...
- **Path Sum II**:寻找所有从根节点到叶节点的路径,其路径和等于给定的目标值。 - **Flatten Binary Tree to Linked List**:将二叉树展平为单链表。 - **Validate Binary Search Tree**:验证一个二叉树是否...
31. Binary Tree Maximum Path Sum:二叉树中的最大路径和。 32. Binary Tree Upside Down:将一个二叉树进行翻转。 【位操作】 33. Single Number:找出数组中唯一不出现一次的数字。 34. Single Number II:找出...
再如,"Binary Tree Maximum Path Sum"问题,要求找到二叉树中从根节点到叶子节点的最大路径和。这个问题可以通过深度优先搜索或广度优先搜索结合动态规划来解决。 GuaZiDou的解决方案不仅包含了代码实现,通常还...
- Minimum Path Sum: 在一个m×n的网格中,找到从左上角到右下角的路径,使得路径上的数字总和最小。 - Plus One: 给定一个由整数组成的非空数组,表示一个非负整数,将这个整数加一。 - Add Binary: 给定两个二进制...
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 $number 题号代表经典 LeetCode 题目,$number.$number ...LeetCode ...Sum ...Sum ...Sum ...Path Sum 73
多线程 leetcode 前言 每天刷点leetcode,基于java语言...Path Sum II Copy List with Random Pointer Building H2O Fizz Buzz Multithreaded hard Merge k Sorted Lists Reverse Nodes in k-Group Trapping Rain Water