【剑指offer】面试题24:二叉搜索树的后续前序遍历序列
2014-06-25 23:02
302 查看
分析:
前序: 根 左 右
后序: 左 由 根
二叉搜索树: 左 < 根 < 右
那么这就很明显了。
前序: 根 左 右
后序: 左 由 根
二叉搜索树: 左 < 根 < 右
那么这就很明显了。
def ifpost(postArray, start, end): #one or "" is true if(end - start <= 1): return True i = start #left branch whose value < root while i <= end and postArray[i] < postArray[end]: i += 1 #partion of left and right branch part = i #right branch whose value > root while i <= end and postArray[i] > postArray[end]: i += 1 #not all right part > root if(i < end): return False return ifpost(postArray, start, part - 1) and ifpost(postArray, part, end - 1)
def ifpreorder(preArray, start, end): if(end - start <= 1): return True i = end while(i >= start and preArray[i] > preArray[start]): i -= 1 partition = i while(i >= start and preArray[i] < preArray[start]): i -= 1 if(i > start): return False return ifpreorder(preArray, start + 1, partition) and \ ifpreorder(preArray, partition + 1, end)
相关文章推荐
- 剑指offer 面试题24 判断二叉搜索树后续遍历序列
- 【剑指Offer】面试题24:二叉搜索树的后续遍历序列
- 剑指offer-面试题24-二叉搜索树的后序遍历序列
- 剑指offer-题24:二叉搜索树的后续遍历序列
- [剑指offer][面试题24]二叉搜索树的后序遍历序列
- 剑指offer-面试题24 二叉搜索树的后序遍历序列
- 【剑指offer】4.3举例让抽象问题具体化——面试题24:二叉搜索树的后序遍历序列
- 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解
- 【剑指offer】面试题24:二叉搜索树的后序遍历序列
- 剑指Offer----面试题24:二叉搜索树的后序遍历序列
- 剑指offer-面试题24:二叉搜索树的后序遍历序列
- 剑指Offer_面试题24_二叉搜索树的后序遍历序列
- 剑指offer面试题24:二叉搜索树的后序遍历序列
- 剑指offer——面试题24:二叉搜索树的后序遍历序列
- 剑指Offer之面试题24:二叉搜索树的后序遍历序列
- 剑指offer面试题24:二叉搜索树的后序遍历序列
- 剑指offer_面试题24_二叉搜索树的后序遍历序列(递归)
- 剑指offer面试题 二叉搜索树的后续遍历序列
- 剑指offer面试题24-二叉搜索树的后序遍历序列
- 剑指Offer面试题24二叉搜索树的后序遍历序列(递归),面试题25二叉树中和为某一值的路径(栈)