path-sum Java code

本文介绍了一种算法,用于判断给定的二叉树是否存在从根节点到叶子节点的路径,使得路径上所有节点值之和等于给定的目标值。通过递归遍历的方法实现这一目标。

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 andsum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root==null)
          return false;
        int now=0;
        return  digui(root,sum,now);
    }
    boolean digui(TreeNode root, int sum,int now){
         now+=root.val;
        if(sum==now&&root.left==null&&root.right==null){
            return true;
        }
        boolean left=false,right=false;

         if(root.left!=null)
             left= digui(root.left,sum,now);
        if(root.right!=null)
             right= digui(root.right,sum,now);
        return left||right;

    }
}
$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、付费专栏及课程。

余额充值