[leetcode/lintcode 题解] 字节跳动面试真题:路径总和 II

简介: [leetcode/lintcode 题解] 字节跳动面试真题:路径总和 II

描述
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。

叶子节点是指没有子节点的节点。

在线评测地址:领扣题库官网

样例1
输入: root = {5,4,8,11,#,13,4,7,2,#,#,5,1}, sum = 22
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1
输出: [[5,4,11,2],[5,8,4,5]]
解释:
两条路径之和为 22:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
样例2
输入: root = {10,6,7,5,2,1,8,#,9}, sum = 18
              10
             /  \
            6    7
          /  \   / \
          5  2   1  8
           \ 
            9 
输出: [[10,6,2],[10,7,1]]
解释:
两条路径之和为 18:
10 + 6 + 2 = 18
10 + 7 + 1 = 18

当访问的节点是叶子节点的时候,新建一个列表,插入到result中,然后返回result。 分别遍历左右子树的节点,然后将他们分别插入到叶子节点之前。

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * @param root: a binary tree
     * @param sum: the sum
     * @return: the scheme
     */
    vector<vector<int>> pathSum(TreeNode * root, int sum) {
        // Write your code here.
        vector<int> path;
        vector<vector<int>> Spath;
        int weight = 0;
        findpath(Spath,root,sum,path,weight);
        return Spath;
    }
    void findpath(vector<vector<int>> &Spath,TreeNode* root,int sum,vector<int> &path,int weight)
    {
        
        if(root == NULL){
            return;
        }
        weight = weight + root->val;
        path.push_back(root->val);
        if(weight == sum && root->left == NULL && root->right == NULL)
        {
            Spath.push_back(path);
            weight = weight - root->val;
            path.pop_back();
            return;
        }
        findpath(Spath,root->left,sum,path,weight);
        findpath(Spath,root->right,sum,path,weight);
        path.pop_back(); 
    }
};

更多题解参考:九章官网solution

相关文章
|
1月前
|
机器人 Python
【Leetcode刷题Python】62. 不同路径
LeetCode 62题 "不同路径" 的Python解决方案,使用动态规划算法计算机器人从网格左上角到右下角的所有可能路径数量。
38 0
|
1月前
|
存储 Python
【Leetcode刷题Python】64. 最小路径和
一种使用动态规划解决LeetCode上64题“最小路径和”的Python实现方法,通过维护一个一维数组来计算从网格左上角到右下角的最小路径总和。
24 1
【Leetcode刷题Python】64. 最小路径和
|
27天前
|
存储 算法 Linux
LeetCode第71题简化路径
文章讲述了LeetCode第71题"简化路径"的解题方法,利用栈的数据结构特性来处理路径中的"."和"..",实现路径的简化。
LeetCode第71题简化路径
|
27天前
|
算法
LeetCode第64题最小路径和
LeetCode第64题"最小路径和"的解题方法,运用动态规划思想,通过构建一个dp数组来记录到达每个点的最小路径和,从而高效求解。
LeetCode第64题最小路径和
|
1月前
|
算法 JavaScript Python
【Leetcode刷题Python】79. 单词搜索和剑指 Offer 12. 矩阵中的路径
Leetcode第79题"单词搜索"的Python解决方案,使用回溯算法在给定的二维字符网格中搜索单词,判断单词是否存在于网格中。
21 4
|
1月前
|
存储 Python
【Leetcode刷题Python】滑雪路径消耗时间:Testing Round #16 (Unrated) C. Skier
Leetcode题目"Testing Round #16 (Unrated) C. Skier"的Python解决方案,题目要求计算给定滑雪路径字符串的总耗时,其中未走过的边耗时5秒,走过的边耗时1秒。
33 4
|
1月前
|
存储 Python
【Leetcode刷题Python】1496.判断路径是否相交
Leetcode第1496题"判断路径是否相交"的Python代码实现,通过使用字典存储方向和集合记录访问过的坐标点来检测路径是否与自身相交。
33 2
|
1月前
|
机器人 Python
【Leetcode刷题Python】63. 不同路径 II
LeetCode 63题 "不同路径 II" 的Python解决方案,使用动态规划算法计算在有障碍物的网格中从左上角到右下角的不同路径数量。
18 1
|
27天前
|
开发者 索引 Python
这些年背过的面试题——LeetCode
本文是技术人面试系列LeetCode篇,一文带你详细了解,欢迎收藏!
|
1月前
|
Python
【Leetcode刷题Python】120. 三角形最小路径和
LeetCode 120题 "三角形最小路径和" 的Python实现,使用动态规划算法找出从三角形顶部到底部的最小路径和。
15 0