题目链接:点击打开链接
题目大意:略
解题思路
相关企业
- 微软(Microsoft)
- 字节跳动
AC 代码
- Java
classSolution { publicbooleanverifyPostorder(int[] postorder) { returnrecur(postorder, 0, postorder.length-1); } booleanrecur(int[] postorder, inti, intj) { if(i>=j) returntrue; intp=i; while(postorder[p] <postorder[j]) p++; intm=p; while(postorder[p] >postorder[j]) p++; returnp==j&&recur(postorder, i, m-1) &&recur(postorder, m, j-1); } }
- C++
classSolution { public: boolverifyPostorder(vector<int>&postorder) { returnrecur(postorder, 0, postorder.size() -1); } private: boolrecur(vector<int>&postorder, inti, intj) { if(i>=j) returntrue; intp=i; while(postorder[p] <postorder[j]) p++; intm=p; while(postorder[p] >postorder[j]) p++; returnp==j&&recur(postorder, i, m-1) &&recur(postorder, m, j-1); } };