path-sum Java code

本文介绍了一种算法,用于查找二叉树中所有从根节点到叶子节点的路径,其中路径上节点值的总和等于给定的目标值。通过递归方式遍历二叉树,实现对路径的有效搜索。

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

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.*;
public class Solution {
    public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
        ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
        if(root == null)return ans;
        if(root.left == null){
            if(root.right == null){
                if(root.val != sum)return ans;
                ArrayList<Integer> list = new ArrayList<Integer>();
                list.add(root.val);
                ans.add(list);
                return ans;
            }else{
                ArrayList<ArrayList<Integer>> right = pathSum(root.right, sum - root.val);
                for(ArrayList<Integer> list : right){
                    list.add(0, root.val);
                    ans.add(list);
                }
                return ans;
            }
        }else{
            if(root.right == null){
                ArrayList<ArrayList<Integer>> left = pathSum(root.left, sum - root.val);
                for(ArrayList<Integer> list : left){
                    list.add(0, root.val);
                    ans.add(list);
                }
                return ans;
            }else{
                ArrayList<ArrayList<Integer>> left = pathSum(root.left, sum - root.val);
                for(ArrayList<Integer> list : left){
                    list.add(0, root.val);
                    ans.add(list);
                }
                ArrayList<ArrayList<Integer>> right = pathSum(root.right, sum - root.val);
                for(ArrayList<Integer> list : right){
                    list.add(0, root.val);
                    ans.add(list);
                }
                return ans;

            }
        }
    }
}
$checkpath="C:\healthcheck\"+$(hostname)+"_"+$(Get-Date -Format yyyymmdd) $checkpath if (test-path -path $checkpath){ "Path exist" } else { New-Item -ItemType Directory -Path $checkpath } $(hostname)+" health report about "+$(Get-Date)>> $checkpath\checkrecord.log "`n#check date" >> $checkpath\checkrecord.log Get-Date -Format yyyyddmm >> $checkpath\checkrecord.log "`n#check OS" >> $checkpath\checkrecord.log systeminfo | findstr "OS" >> $checkpath\checkrecord.log "`n#check java" >> $checkpath\checkrecord.log Get-Command java | Select-Object version >> $checkpath\checkrecord.log "`n#check disk" >> $checkpath\checkrecord.log Get-PSDrive | findstr "C:\" >> $checkpath\checkrecord.log "`n#check CPU" >> $checkpath\checkrecord.log Get-WmiObject Win32_Processor |Measure-Object -Property LoadPercentage -Average | select Average >> $checkpath\checkrecord.log "`n#check memory" >> $checkpath\checkrecord.log Get-WmiObject Win32_PhysicalMemory |Measure-Object -Property capacity -Sum | %{$_.sum/1Mb} >> $checkpath\checkrecord.log "`n#check IP" >> $checkpath\checkrecord.log ipconfig >> $checkpath\checkrecord.log "`n#check port opening" >> $checkpath\checkrecord.log netstat -an | findstr 443 >> $checkpath\checkrecord.log netstat -an | findstr 1433 >> $checkpath\checkrecord.log "`n#check service" >> $checkpath\checkrecord.log net start | findstr Apa* >> $checkpath\checkrecord.log net start | findstr SQL* >> $checkpath\checkrecord.log #& "C:\Program Files\Apache Software Foundation\Tomcat 10.1.19\bin\version.bat" > $checkpath\tomcat_ver.log "C:\Users\Administrator\Desktop\apache-tomcat-9.0.90\bin\version.bat" > $checkpath\tomcat_ver.log #$last_log_file=$(Get-Item "C:\Program Files\Apache Software Foundation\Tomcat 9.0.69\logs\catalina*" | Sort-Object LastWriteTime -Descending |Select-Object -Last 1|Select-Object Name) #$last_log_file #Get-ChildItem "C:\Program Files\Apache Software Foundation\Tomcat 9.0.69\logs\" -File|findstr catalina* | Sort-Object LastWriteTime -Descending |Select-Object -Last 1|Select-Object BaseName #Get-Item "C:\Program Files\Apache Software Foundation\Tomcat 9.0.69\logs\catalina*" | Sort-Object LastWriteTime -Descending |Select-Object -Last 1|Select-Object Name #Get-Content -path "C:\Program Files\Apache Software Foundation\Tomcat 9.0.69\logs\catalina..log" -tail 1000 > $checkpath\tomcat.log #[Net.ServicePointManager]::ServerCertificateValidationCallback ={$ture} #Invoke-WebRequest -Uri http://10.56.7.21/bcrf -UseBasicParsing | Select-Object -expand statusCode > $checkpath\checkweb.log Get-Content -path "C:\bcrfLog\logs\debug.log" -tail 1000 > $checkpath\debug.log # check backup log ls "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\" > $checkpath\dbback.log #check db log Get-Content -path "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Log\ERRORLOG" -tail 1000 > $checkpath\db.log 帮我解释这段脚本内容,并根据这个脚本内容给出一份优化建议以及优化后的脚本
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值