面试题24:二叉搜索树的后序遍历序列

链接: https://www.nowcoder.com/questionTerminal/a861533d45854474ac791d90e447bafd
来源:牛客网

java.util.Arrays;
public class Solution {
    public static boolean VerifySquenceOfBST( int [] sequence) {
         if (sequence== null ||sequence.length== 0 )
             return false ;
         int root=sequence[sequence.length- 1 ];
         int i= 0 ;
         for (;i<sequence.length- 1 ;i++){
             if (sequence[i]>root){
                 break ;
             }
         }
         int j=i;
         for (;j<sequence.length- 1 ;j++){
             if (sequence[j]<root)
                 return false ;
         }
         booleanleft=true;
        booleanright=true;
         if (i> 0 ){
             left=VerifySquenceOfBST(Arrays.copyOfRange(sequence, 0 , i));
         }
         if (i<sequence.length- 1 )
             right=VerifySquenceOfBST(Arrays.copyOfRange(sequence, i, sequence.length- 1 ));
         return (left&&right);
 
     }
}

copyOfRange
public static long[] copyOfRange(long[] original,int from, int to)
  
  
original - the array from which a range is to be copied
from - the initial index of the range to be copied, inclusive 包括
to - the final index of the range to be copied, exclusive不包括 . (This index may lie outside the array.)

数学里的inclusive和exclusive有什么特殊概念嘛.

是不是集合里面的东西,比如说

x属于(1,3) 就是  1<x<3  代表exclusive

x属于[1,3] 是 1<=x<=3  代表inclusive

break 是跳出 最内层循环 在else中,也同样是跳出 最内层循环
循环有for   while  do-while
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值