题解 | #二叉搜索树的后序遍历序列 重点在于扣边界条件#

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

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

import java.util.*;

public class Solution {

public boolean VerifySquenceOfBST(int [] sequence) {
    if(sequence.length == 0){
        return false;
    }
    int[] tmp = new int[sequence.length];
    for(int i = 0; i< sequence.length; i++){
        tmp[i] = sequence[i];
    }
    Arrays.sort(tmp);
    return verify(sequence, tmp);
}
public boolean verify(int[] sequence, int[] tmp){
    if(sequence.length == 0 && tmp.length == 0){return true;}
    if(sequence.length != tmp.length){return false;}
    if(sequence.length == tmp.length && sequence.length == 1 && sequence[0] == tmp[0]){return true;}
    for(int i = 0; i < tmp.length; i++){
        if(tmp[i] == sequence[sequence.length - 1]){
            return verify(Arrays.copyOfRange(sequence, 0, i), Arrays.copyOfRange(tmp, 0, i))
                && verify(Arrays.copyOfRange(sequence, i, sequence.length - 1), Arrays.copyOfRange(tmp, i + 1, sequence.length));
        }
    }
    return false;
}

}

全部评论

相关推荐

挥毫自在:想白嫖你呢
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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