您的位置:首页 > 其它

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

2016-07-25 17:32 405 查看
来源:牛客网 《剑指offer》

题目描述

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

先复习一下二叉搜索树(来自维基百科):

二叉查找树(英语:Binary Search Tree),也称二叉搜索树、有序二叉树(英语:ordered binary tree),排序二叉树(英语:sorted binary tree),是指一棵空树或者具有下列性质的二叉树

任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;

任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;

任意节点的左、右子树也分别为二叉查找树;

没有键值相等的节点。

后序遍历是先访问左子树,然后右子树,最后根节点。

下图的后序遍历序列为:4,6,7,5,12,20,15,10.

因此思路是,先遍历序列,与根节点比较,将序列分为左子树和右子树,其中左子树的值都必须<根节点,右子树的值都必须>根节点。递归上述过程。

public class TT {
static int time=0;

public static void main(String[] args) {
int[] a={4,6,7,5,12,20,15,10};
// int[] a={5,4,3,2,1};

System.out.println(VerifySquenceOfBST(a));
}

public static boolean VerifySquenceOfBST(int [] sequence) {
//if(sequence==null) return false;
if(sequence.length==0) return false;
return Core(sequence, 0, sequence.length-1);
}

public static boolean Core(int[] a, int start, int end){
//if(start==end) return true;
if(start>=end) return true;

int mid=getMid(a, start, end);
System.out.println(start+" "+end+" "+mid);
for(int i=start; i<= mid; ++i)
if(a[i]>a[end]) return false;
for(int i=mid+1; i<= end-1; ++i)
if(a[i]<a[end]) return false;

return (Core(a, start, mid) && Core(a, mid+1, end-1));
}

// 4 6 7 5 | 12 20 15 10
// root=10
public static int getMid(int[] a, int start, int end){
for(int i=start; i<=end; ++i){
if(a[i]>a[end]) {
// if(i==start) return start; // left subtree is null
// else return i-1;
return i-1; // if left subtree is null, i.e. i=0, return value is -1
}
}

return end-1; // if the right subtree is null
}
}


View Code
碰到的细节问题有:

(1)数组==null 与 长度为0并不一样

//if(sequence==null) return false;
if(sequence.length==0) return false;

int[] a={};
a 的长度为0,但a!=null。


(2)边界条件

//if(start==end) return true;
if(start>=end) return true;


for 循环初始化语句i=start;而不是i=0;
43行处return end-1,而不是 a.length-1.


(3) getMid 函数用于将序列分为左子树和右子树,返回左子树的最后一个节点的索引。

当左子树为空时,应该返回-1. 而我一开始的代码返回0. (上述代码中已注释掉)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: