本文共 412 字,大约阅读时间需要 1 分钟。
我们知道,后续遍历数组最后一个元素就是根节点。
由于二叉搜索树的左子数比根节点小,右子数比根节点大。所以由此可以确定出左右子树,然后递归的做下去。
如何判断不是二叉搜索树,先确定左子数,然后判断右子数是否都大于根节点。
class Solution {public: bool verifyPostorder(vector & postorder) { return dfs(postorder,0,postorder.size()-1); } bool dfs(vector & postorder, int left, int right){ if(left>=right) return true; int val = postorder[right]; int k = left; while(k
转载地址:http://sjqv.baihongyu.com/