标签:.com div lse ret turn system 后序 array 判断
题目:
输入一个数组,判断该数组是不是某二叉搜索树的后序遍历结果。
解答:
1 public class Solution { 2 3 public static void main(String[] args) { 4 int[] array = {5,7,6,9,11,10,8}; 5 6 boolean b = verfiySequenceOfBST(array, 0, 6); 7 System.out.println(b); 8 } 9 10 private static boolean verfiySequenceOfBST(int[] array, int start, int end) { 11 if(array == null || start > end || start < 0 || end < 0) { 12 return false; 13 } 14 15 if(start == end) { 16 return true; 17 } 18 19 int root = array[end]; 20 for(; i <= end; i++) { 21 if(array[i] > root) { 22 break; 23 } 24 } 25 26 int j = i; 27 for(; j <= end; j++) { 28 if(array[j] < root) { 29 return false; 30 } 31 } 32 33 boolean left = true; 34 35 if(i > start) { 36 left = verfiySequenceOfBST(array, start, i-1); 37 } 38 39 boolean right = true; 40 41 if(i < end) { 42 right = verfiySequenceOfBST(array, i, end-1); 43 } 44 45 return left&&right; 46 47 } 48 }
标签:.com div lse ret turn system 后序 array 判断
原文地址:https://www.cnblogs.com/wylwyl/p/10369095.html