zoukankan      html  css  js  c++  java
  • Lc113_路径总和 II

    package com.example.demo;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * 113. 路径总和 II
     * 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
     * <p>
     * 说明: 叶子节点是指没有子节点的节点。
     * <p>
     * 示例:
     * 给定如下二叉树,以及目标和 sum = 22,
     * <p>
     * 5
     * / 
     * 4   8
     * /   / 
     * 11  13  4
     * /      / 
     * 7    2  5   1
     * 返回:
     * <p>
     * [
     * [5,4,11,2],
     * [5,8,4,5]
     * ]
     */
    public class Lc113 {
    
          //将路径记录成字符串,在进行格式转换,可以考虑深拷贝
        public static List<List<Integer>> pathSum(TreeNode root, int sum) {
            LinkedList<String> linkedList = new LinkedList<>();
            calc(root, 0, "", linkedList, sum);
            linkedList.forEach(n->{
                System.out.println(n);
            });
            List<List<Integer>> lists = new ArrayList<>();
            linkedList.forEach(n->{
                String[] temp = n.split(",");
                List<Integer> temps =new ArrayList<>();
                for (int i = 0; i < temp.length; i++) {
                    temps.add(Integer.valueOf(temp[i]));
                }
                lists.add(temps);
            });
    
            return lists;
        }
    
        public static void calc(TreeNode root, int res, String path, LinkedList<String> paths, int sum) {
            if (root != null) {
                path += root.val;
                res += root.val;
                if (root.left == null && root.right == null && res == sum) {
    //                System.out.println(path);
    //                System.out.println(res);
                    paths.add(path);
                } else {
                    path += ",";
                    calc(root.left, res, path, paths, sum);
                    calc(root.right, res, path, paths, sum);
                }
            }
        }
    
        public static void main(String[] args) {
    
            Integer[] arr = new Integer[]{1, 2, 3, 4, 5, 6, 7};
            TreeNode root = CreateNode.createTree(arr).get(0);
            pathSum(root, 7).forEach(n->{
               n.forEach(m->{
                   System.out.println(m);
               });
            });
        }
    
    }
    
    
  • 相关阅读:
    5.2 TensorFlow:模型的加载,存储,实例
    5.2 TensorFlow:模型的加载,存储,实例
    卷积神经网络(cnn)的体系结构
    卷积神经网络(cnn)的体系结构
    Python学习笔记(四) 函数
    Python学习笔记(三) 字典
    Python学习笔记(二) 字符串
    Python学习笔记(一) 列表和元组
    Linux学习笔记(七) 查询系统
    Linux学习笔记(六) 进程管理
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/13140545.html
Copyright © 2011-2022 走看看