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

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

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

function split(list){
    //出口
    if(list.length == 0){
        return true;
    }
    //浅拷贝
    let list_copy1 = [];
    let list_copy2 = [];
    for(let i=0;i<list.length;i++){
        list_copy1.push(list[i]);
        list_copy2.push(list[i]);
    }
    let count = 0;
    let root = list[list.length-1];
    while(list[count] < list[list.length-1]){
        count += 1;
    }
    for(let i=count;i<list.length-1;i++){
        if(list[i] <= root){
            return false;
        }
    }
    let left = [];
    let right = []
    // if()
    left = list_copy1.splice(0,count);
    if(count < list.length - 1){
        right = list_copy2.splice(count,list.length-1);
    }
    return split(left) && split(right);
}
function VerifySquenceOfBST(sequence)
{
    // write code here
    if(sequence.length == 0){
        return false;
    }
    return split(sequence);
}
module.exports = {
    VerifySquenceOfBST : VerifySquenceOfBST
};

#我的实习求职记录#
全部评论

相关推荐

努力成为C语言高手:质疑大祥老师,理解大祥老师,成为大祥老师
点赞 评论 收藏
分享
我即大橘:耐泡王
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务