码迷,mamicode.com
首页 > 其他好文 > 详细

【Leetcode】113Path Sum II

时间:2018-02-07 00:40:29      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:str   post   where   equals   rgs   hhhh   iter   类型   strong   

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]
]

Tips:在112题的基础上,加深难度,本题要求输出和为sum的树中的所有路径。

本题要注意List<Integer> 是 List<List<Integer>> 的一个元素。要求出所有路径,需要每找到一条List<Integer>类型的路径,就将其添加到 List<List<Integer>>集合中。

package medium;

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

public class L113PathSumII {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> path = new ArrayList<>();
        List<Integer> oneline = new ArrayList<>();
        if (root == null)
            return path;
        int ans = 0;
        hasPathSumCore(root, sum, path, ans,oneline);
        return path;
    }

    private List<List<Integer>> hasPathSumCore(TreeNode root, int sum, List<List<Integer>> path, int ans,List<Integer> oneline) {
        ans += root.val;
        oneline.add(root.val);
        if (ans == sum && root.left == null && root.right == null) {
            path.add(new ArrayList<Integer>(oneline));
        }
        if (root.left != null) {
             hasPathSumCore(root.left, sum,path, ans,oneline);
        }
        if (root.right != null) {
            hasPathSumCore(root.right, sum,path, ans,oneline);
        }
        ans -= root.val;
        oneline.remove(oneline.size()-1);
        return path;

    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(5);
        TreeNode node1 = new TreeNode(4);
        TreeNode node2 = new TreeNode(8);
        TreeNode node3 = new TreeNode(11);
        TreeNode node4 = new TreeNode(13);
        TreeNode node5 = new TreeNode(4);
        TreeNode node6 = new TreeNode(7);
        TreeNode node7 = new TreeNode(2);
        TreeNode node8 = new TreeNode(1);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;
        node5.right = node8;

        node1.right = null;
        node6.left = null;
        node6.right = null;
        node7.left = null;
        node7.right = null;
        node4.left = null;
        node4.right = null;
        node5.left = null;
        node8.left = null;
        node8.right = null;
        int sum = 22;

        L113PathSumII l113 = new L113PathSumII();
        List<List<Integer>> ans =new ArrayList();
        ans=l113.pathSum(root, sum);
        for(int i=0;i<ans.size();i++){
            List<Integer> iter=ans.get(i);
            for(int j=0;j<iter.size();j++){
                System.out.println(iter.get(j));
            }
            
        }
        System.out.println("HHHHHHHHHHHHH");
        TreeNode root1 = new TreeNode(-2);
        TreeNode root2 = new TreeNode(-3);
        root1.left = null;
        root1.right = root2;
        root2.left = null;
        root2.right = null;
        ans=l113.pathSum(root1, -5);
        for(int i=0;i<ans.size();i++){
            List<Integer> iter=ans.get(i);
            for(int j=0;j<iter.size();j++){
                System.out.println(iter.get(j));
            }
            
        }
    }
}

 

【Leetcode】113Path Sum II

标签:str   post   where   equals   rgs   hhhh   iter   类型   strong   

原文地址:https://www.cnblogs.com/yumiaomiao/p/8424568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!