def buildTree(self, inorder, postorder):
        if not postorder:
            return None

        root = TreeNode(postorder.pop())
        idx = inorder.index(root.val)
        root.left = self.buildTree(inorder[:idx], postorder[:idx])
        root.right = self.buildTree(inorder[idx + 1:], postorder[idx:])

        return root
예제 #2
0
    def buildTree(self, preorder, inorder):

        if not preorder:
            return None

        root = TreeNode(preorder[0])
        idx = inorder.index(root.val)
        root.left = self.buildTree(preorder[1:idx + 1], inorder[:idx])
        root.right = self.buildTree(preorder[idx + 1:], inorder[idx + 1:])

        return root
    def sortedArrayToBST(self, A):
        if not A:
            return None

        root_index = len(A) // 2

        root = TreeNode(A[root_index])
        root.left = self.sortedArrayToBST(A[:root_index])
        root.right = self.sortedArrayToBST(A[root_index + 1:])

        return root
예제 #4
0
    def buildTree(self, A):

        if not A:
            return None

        import operator
        max_index, max_value = max(enumerate(A), key=operator.itemgetter(1))
        #d_keys = list(d.keys())
        #u = max(d_keys, key=(lambda k: d[k]))

        root = TreeNode(max_value)
        root.left = self.buildTree(A[:max_index])
        root.right = self.buildTree(A[max_index + 1:])

        return root
예제 #5
0
    # @param A : root node of tree
    # @return an integer
    def isSymmetric(self, A):
        if not A:
            return 1

        return int(self._isSymetric2(A, A))

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

if __name__ == '__main__':
    #r1 = TreeNode(int(sys.argv[1]))
    #for arg in range(2, len(sys.argv), 1):
    #    insertNode(r1, TreeNode(int(sys.argv[arg])))

    r1 = TreeNode(2)
    node1 = TreeNode(1)
    node2 = TreeNode(1)

    r1.left = node1
    r1.right = node2

    s = Solution()
    if s.isSymmetric(r1):
        print("Both trees are Identical Binary Trees")
    else:
        print("Both trees are not Identical Binary Trees")


    # @param B : integer
    # @return a list of list of integers
    def pathSum(self, A, B):
        return self._pathSum(A, [], 0, B)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

if __name__ == '__main__':

    s = Soution()
    t = TreeNode(5)
    t.left = TreeNode(4)
    t.left.left = TreeNode(11)
    t.left.left.left = TreeNode(7)
    t.left.left.right = TreeNode(2)
    t.right = TreeNode(8)
    t.right.left = TreeNode(13)
    t.right.right = TreeNode(4)
    t.right.right.left = TreeNode(5)
    t.right.right.right = TreeNode(1)
    print(s.pathSum(t, 22))

#               5
#              / \
#             4   8
#            /   / \
#           11  13  4
#          /  \    / \
#         7    2  5   1
예제 #7
0
        self._inorder(root.left, pfs)

        if not pfs[0]:
            pfs[0] = root
        else:
            if root.val < pfs[0].val:
                if not pfs[1]:
                    pfs[1] = pfs[0]
                pfs[2] = root
            pfs[0] = root

        self._inorder(root.right, pfs)

    # @param A : root node of tree
    # @return a list of integers
    def recoverTree(self, A):
        pfs = [None, None, None]
        self._inorder(A, pfs)
        print([pfs[1].val, pfs[2].val])
        return sorted([pfs[1].val, pfs[2].val])


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

if __name__ == '__main__':
    r = TreeNode(3)
    r.left = TreeNode(1)
    r.right = TreeNode(2)

    s = Solution()
    print(s.recoverTree(r))