zoukankan      html  css  js  c++  java
  • 【Leetcode】113Path 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]
    ]
    

    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));
                }
                
            }
        }
    }
  • 相关阅读:
    【转载】SAP_ECC6.0_EHP4或SAP_ECC6.0_EHP5_基于Windows_Server_2008R2_和SQL_server_2008下的安装
    使用delphi 开发多层应用(二十四)KbmMW 的消息方式和创建WIB节点
    使用delphi 开发多层应用(二十三)KbmMW 的WIB
    实现KbmMw web server 支持https
    KbmMW 服务器架构简介
    Devexpress VCL Build v2014 vol 14.1.1 beta发布
    使用delphi 开发多层应用(二十二)使用kbmMW 的认证管理器
    KbmMW 4.50.00 测试版发布
    Basic4android v3.80 beta 发布
    KbmMW 认证管理器说明(转载)
  • 原文地址:https://www.cnblogs.com/yumiaomiao/p/8424568.html
Copyright © 2011-2022 走看看