第 9 题判断整数序列是不是二元查找树的后序遍历成果题目:输入一种整数数组,判断该数组是不是某二元查找树的后序遍历的成果
假如是返回 true,否则返回 false
例如输入 5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历成果:8/ \6 10/ \ / \5 7 9 11因此返回 true
假如输入 7、4、6、5,没有哪棵树的后序遍历的成果是这个序列,因此返回 false
ANSWER:This is an interesting one
There is a traditional question that requires the binary tree to be re-constructed from mid/post/pre order results
This seems similar
For the problems related to (binary) trees, recursion is the first choice
In this problem, we know in post-order results, the last number should be the root
So we have known the root of the BST is 8 in the example
So we can split the array by the root
int isPostorderResult(int a[], int n) { return helper(a, 0, n-1);}int helper(int a[], int s, int e) { if (e==s) return 1; int i=e-1; while (a[e]>a[i] && i>=s) i--; if (