本文主要是介绍剑指offer33.二叉搜索树的后序遍历序列。简单易懂0ms,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
class Solution {public boolean verifyPostorder(int[] postorder) {return dfs(postorder, 0, postorder.length - 1);}boolean dfs(int[] postorder, int left, int right){if(left >= right) return true;// 搜索树的性质int pointer = left;while(postorder[pointer] < postorder[right]) ++pointer;int mid = pointer;while(postorder[pointer] > postorder[right]) ++pointer;// 判断此树以及左右子树是否正确return pointer == right && dfs(postorder, left, mid - 1)&&dfs(postorder, mid, right - 1);}
}
这篇关于剑指offer33.二叉搜索树的后序遍历序列。简单易懂0ms的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!