会不断更新!!!!!!
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true
,否则返回 false
。假设输入的数组的任意两个数字都互不相同。
解法:这个题目注意的点:
1 先找到根节点,左子树在前面,右子树在其后,然后递归判断。
2 如果是空数组,返回的应该是true,我先以为空数组是false,后来回想起二叉搜索树的定义:可以是一棵空树。
class Solution {
public boolean verifyPostorder(int[] postorder) {
if(postorder.length==0) return true;
else return verify(postorder,0,postorder.length-1);
}
public boolean verify(int[] postorder,int start,int end) {
//1 terminator
if(start==end) return true;