题解 | #二叉搜索树的后序遍历序列#

二叉搜索树的后序遍历序列

http://www.nowcoder.com/practice/a861533d45854474ac791d90e447bafd

public class Solution { public boolean VerifySquenceOfBST(int [] sequence) { int len = sequence.length; if(len == 0){ return false; } return help(sequence,0,len-1);

}
public boolean help (int[] sequence,int start,int end){
    if(start >= end){
        return true;
    }
    int root = sequence[end];
    int index = -1;
    for(int i = 0; i < end;i++){
        if(sequence[i] < root){
            index++;
        }
    }
    for(int i = index+1;i < end;i ++){
        if(sequence[i] < root){
            return false;
        }
    }
    return help(sequence,start,index) && help(sequence,index+1,end -1);
}

}

全部评论

相关推荐

2025-12-16 15:40
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务