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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 5
0
 def increasingBST2(self, root):
     """
     :type root: TreeNode
     :rtype: TreeNode
     """
     tree = TreeNode()
     self.InOrder(root, tree)
     return tree
Exemplo n.º 6
0
        stack, ans = list(), list()
        i = 0

        if A:
            stack.append(A)

        while stack:
            node = stack[-1]
            if not node.left or hasattr(node.left, 'visitedp'):
                if not node.right or hasattr(node.right, 'visitedp'):
                    ans.append(stack.pop().val)
                    node.visitedp = True
                else:
                    stack.append(node.right)
            else:
                stack.append(node.left)

        return ans

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

if __name__ == '__main__':
    # Input DATA to practice: 11 2 9 13 57 25 17 1 90 3
    r = TreeNode(int(sys.argv[1]))
    for arg in range(2, len(sys.argv), 1):
        insertNode(r, TreeNode(int(sys.argv[arg])))

    s = Solution()
    L = s.postorderTraversal(r)
    print("Post Order Traversal: {}".format(L))
Exemplo n.º 7
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")


        return ans

    # @param A : root node of tree
    # @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
#            /   / \
Exemplo n.º 9
0
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None


class Solution(object):
    # @param A : root node of tree
    # @return an integer
    def maxDepth(self, A):
        if A is None:
            return 0
        return max(self.maxDepth(A.left), self.maxDepth(A.right)) + 1

    def minDepth(self, root):
        if not root:
            return 0
        if not root.left:
            return self.minDepth(root.right) + 1
        elif not root.right:
            return self.minDepth(root.left) + 1
        else:
            return min(self.minDepth(root.left), self.minDepth(root.right)) + 1


if __name__ == '__main__':
    r = TreeNode(1)
    insertNode(r, TreeNode(2))
    s = Solution()
    print(s.maxDepth(r))
    print(s.minDepth(r))
Exemplo n.º 10
0
            return tail
        res = self.increasingBST(root.left, root)
        root.left = None
        root.right = self.increasingBST(root.right, tail)
        return res

    def increasingBST2(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        tree = TreeNode()
        self.InOrder(root, tree)
        return tree

    def InOrder(self, root, tree):
        if root:
            self.InOrder(root.left, tree)
            tree.append(root.val)
            self.InOrder(root.right, tree)


tree = TreeNode()
a = [5, 3, 6, 2, 4, None, 8, 1, None, None, None, 7, 9]
# b = [i for i in range(2,10)]
for i in a:
    tree.append(i)

tree.printLevelorder(tree)
flatten = Solution().increasingBST(tree)
tree.printLevelorder(flatten)
Exemplo n.º 11
0
                    and self._identicalTrees(a.right, b.right))

        # 3. one empty, one not -- false
        return False

    # @param A : root node of tree
    # @param B : root node of tree
    # @return an integer
    def isSameTree(self, A, B):
        return int(self._identicalTrees(A, B))


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

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])))

    r2 = TreeNode(int(sys.argv[1]))
    for arg in range(2, len(sys.argv), 1):
        insertNode(r2, TreeNode(int(sys.argv[arg])))

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

    #preOrderTraversal(r2)
    #print()
Exemplo n.º 12
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))