判断一个数组是否是一棵树的后续遍历

时间:2022-09-26 20:29:40
public boolean judge (int[] nums) {

if (nums.length == 0 || nums == null)
return true;
int sum = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] >= nums[nums.length - 1]) {
sum = i;
break;
}
}


int[] nums1 = new int[sum];
for (int i = 0; i < nums1.length - 1; i++) {
nums1[i] = nums[i];
}

int[] nums2 = new int[nums.length - 1 - sum];
for (int i = nums1.length; i < nums.length -2; i++) {
if (nums[i] < nums[nums.length - 1])
return false;
nums2[i - nums1.length] = nums[i];
}

boolean r1 = judge(nums1);
boolean r2 = judge(nums2);
if (r1 && r2)
return true;
return false;
}