def _constructHelper(self, iStart, iEnd): if iEnd < iStart: return None root = BinaryTree(self._postorder[self.pIndex]) self.pIndex -= 1 if iStart == iEnd: return root inIndex = self._inorder.index(root.getRootVal()) root.setRightChild(self._constructHelper(inIndex + 1, iEnd)) root.setLeftChild(self._constructHelper(iStart, inIndex - 1)) return root
def _constructHelper(self, levelorder, inorder): if not levelorder: return None root = BinaryTree(levelorder[0]) if len(levelorder) == 1: return root inIndex = inorder.index(root.getRootVal()) left = inorder[:inIndex] right = inorder[inIndex+1:] root.setLeftChild(self._constructHelper(self._getSubList(levelorder, left), left)) root.setRightChild(self._constructHelper(self._getSubList(levelorder, right), right)) return root