【LeetCode-面试算法经典-Java实现】【113-Path Sum II(路径和)】

【113-Path Sum II(路径和II)】


【LeetCode-面试算法经典-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]
]

题目大意

  给定一棵二叉树和一个和,判断从树的根结点到叶子结点的所有结点的和是否等于给定的和,如果等于就记录这条路径。

解题思路

  对树进行遍历,并且使用回溯法进行求解。

代码实现

树结点类

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

算法实现类

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution {
    private List<List<Integer>> result;
    private List<Integer> l;
    private int sum;
    private int curSum = 0;

    public List<List<Integer>> pathSum(TreeNode root, int sum) {

        result = new LinkedList<>();

        if (root != null) {
            this.sum = sum;
            l = new LinkedList<>();
            pathSum(root);
        }

        return result;
    }

    private void pathSum(TreeNode root) {
        if (root != null) {

            l.add(root.val);
            curSum += root.val;

            if (root.left == null && root.right == null && curSum == sum) {
                List<Integer> list = new LinkedList<>();
                for (Integer i : l) {
                    list.add(i);
                }

                result.add(list);
            }

            if (root.left != null) {
                pathSum(root.left);
            }

            if (root.right != null) {
                pathSum(root.right);
            }

            curSum -= root.val;
            l.remove(l.size() - 1); // 删除最后一个
        }
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47438073

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-20 15:48:21

【LeetCode-面试算法经典-Java实现】【113-Path Sum II(路径和)】的相关文章

[LeetCode]题解(python):113 Path Sum II

题目来源 https://leetcode.com/problems/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. 题意分析 Input: a binary tree, sum Output: list of list. Conditions: 给定一个二叉树,将所有root-leaf的路径值和等于sum的路径返

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 路径和

递归先序遍历+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

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 基于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】#113. Path Sum II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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,

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

[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] ] 题意:给定一数,在树中找出所有路径和等于该数的情况.方

113. Path Sum II (Tree; DFS)

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] ] struct TreeNode { int val

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