[LeetCode][Java] 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]
]

题意:

给定一棵二叉树和一个和,找出从根节点到叶子节点的遍历中节点值的和等于给定目标值的所有的路径。

比如:

给定如下的二叉树和sum = 22,

              5
             /             4   8
           /   /           11  13  4
         /  \    /         7    2  5   1

返回

[
   [5,4,11,2],
   [5,8,4,5]
]

算法分析:

* 同题目《Path Sum》相似

* 同样利用递归策略

* 不同之处在于本题需要在遍历的时候记录走寻的路径 dfs

* 但是需要注意的是,递归回溯上来的时候要进行清理工作

AC代码:

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
        ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
        helper(root,sum,new ArrayList<Integer>(),res);
        return res;
    }

    public void helper(TreeNode root,int sum,ArrayList<Integer> cur,ArrayList<ArrayList<Integer>> res)
    {
        if(root==null)
            return;
        //到达叶子节点且根结点的值等于sum
        if(root.left==null && root.right==null && root.val==sum)
        {
            cur.add(root.val);
            res.add(new ArrayList<Integer>(cur));
            //清除操作
            cur.remove(cur.size()-1);
            return;
        }
        //没有到达叶子节点,左子树不为空
        if(root.left!=null)
        {
            cur.add(root.val);
            //对左子树进行递归调用
            helper(root.left,sum-root.val,cur,res);
            //返回时,清除操作
            cur.remove(cur.size()-1);
        }
        //没有到达叶子节点,右子树不为空
        if(root.right!=null)
        {
            cur.add(root.val);
            //对右子树进行递归调用
            helper(root.right,sum-root.val,cur,res);
            //返回时,清除操作
            cur.remove(cur.size()-1);
        }

    }
}

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-10-12 07:30:18

[LeetCode][Java] Path Sum II的相关文章

【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][JAVA] Path Sum I &amp;&amp; II

Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return t

[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

[Leetcode Week14]Path Sum II

Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Example Given the below binary tree and sum = 22 5

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

[C++]LeetCode: 91 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 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

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

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