标签:stat class 判断 public com inf bre 输入 遍历
题目:
输入一个整型数组,判断该数组是不是二叉搜索树的后序遍历结果。
如果是,返回true。否则返回false
解答:
1 public class Solution { 2 3 public static boolean VerifySquenceOfBST(int[] squence) { 4 return VerifySquenceOfBST(squence, squence.length); 5 } 6 7 private static boolean VerifySquenceOfBST(int[] squence, int length) { 8 if(squence == null || length == 0) { 9 return false; 10 } 11 12 int root = squence[length-1]; 13 14 int i = 0; 15 for(; i < length-1; i++) { 16 if(squence[i] > root) { 17 break; 18 } 19 } 20 21 int j = i; 22 for(; j < length-1; j++) { 23 if(squence[j] < root) { 24 return false; 25 } 26 } 27 28 boolean left = true; 29 if(i > 0) { 30 left = VerifySquenceOfBST(squence, i); 31 } 32 33 boolean right = true; 34 if(i < length-1) { 35 right = VerifySquenceOfBST(squence, length-i-1); 36 } 37 38 return left&&right; 39 } 40 }
标签:stat class 判断 public com inf bre 输入 遍历
原文地址:https://www.cnblogs.com/wylwyl/p/10462897.html