面试题33. 二叉搜索树的后序遍历序列

2020-03-19  本文已影响0人  阿星啊阿星

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

题目描述

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。


image
示例:

输入: [1,6,3,2,5]
输出: false

输入: [1,3,2,6,5]
输出: true


提示:
数组长度 <= 1000

转载来源:力扣(LeetCode)


题目分析

  1. 二叉搜索树的特点:
    1.1. 左子树的所有节点永远小于等于父节点
    1.2. 右子树的所有节点永远大于等于右子树
    1.3. 左子树和右子树也是二叉搜索树
  2. 后续遍历的特点:左子树先遍历、右子树后遍历、父节点最后遍历

举例子: [1,3,2,6,5]

    public boolean verifyPostorder(int[] postorder) {
        if(postorder.length == 1) return true;
        return verifyPostorder(postorder,0,postorder.length-1);
    }
    
    //验证当前数是否为二叉搜索树
    public boolean verifyPostorder(int[] postorder,int head,int tail) {
        if(head >= tail) return true;
        int parent = postorder[tail];  
        int rightHead = tail;
        //寻找右子树和左子树的分界点
        while(rightHead >= head){
            if(postorder[rightHead] >= parent)
                rightHead--;
            else
                break;
        }
        rightHead += 1;
        // 验证右子树是否为二叉搜索树
        if(!verifyPostorder(postorder,rightHead,tail-1))
            return false;
        // 验证左子树是否都比父节点小
        if(!verifyLeft(postorder,head,rightHead-1,parent))
            return false;
        // 验证左子树是否为二叉搜索树
        return verifyPostorder(postorder,head,rightHead-1);
    }

    public boolean verifyLeft(int[] postorder,int head,int tail,int parent){
        for(int i = head; i <= tail; i++){
            if(postorder[i] > parent)
                return false;
        }
        return true;
    }
上一篇 下一篇

猜你喜欢

热点阅读