后序数组判断

题目

https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public boolean verifyPostorder(int[] postorder) {
return recur(postorder, 0, postorder.length-1);
}

boolean recur(int[] postorder, int i, int j) {
if (i >= j) return true;
int p = i;
while (postorder[p] < postorder[j]) {
p++;
}
int m = p;
while (postorder[p] > postorder[j]) {
p++;
}
return p == j && recur(postorder, i, m-1) && recur(postorder, m, j-1);
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×