예제 #1
0
    def isSubtree(self, s, t):
        """
        :type s: TreeNode
        :type t: TreeNode
        :rtype: bool
        """
        def check(s, t):
            if s is None and t is None:
                return True
            if s is None or t is None:
                return False
            return (s.val == t.val and check(s.left, t.left) and check(s.right, t.right))

        if not s:
            return
        if check(s, t):
            return True
        if self.isSubtree(s.left, t) or self.isSubtree(s.right, t):
            return True
        return False




from Leetcode_easy.tree import CreateTree
lst = [1,None ,2]
tree = CreateTree.Create(lst)
CreateTree.depth_tree(tree)
print('^^^^^^^^^^^^^^^^^^^^^')
root = tree._root
s = Solution()
예제 #2
0
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution(object):
    def convertBST(self, root):
        def generate_greater_tree(node):
            if not node: return None
            right = generate_greater_tree(node.right)
            self.sum += node.val
            new_node = TreeNode(self.sum)
            new_node.right = right
            new_node.left = generate_greater_tree(node.left)
            return new_node

        self.sum = 0
        return generate_greater_tree(root)


from Leetcode_easy.tree import CreateTree
lst = [2, 0, 3, -4, 1]
tree = CreateTree.Create(lst)
CreateTree.depth_tree(tree)
print('^^^^^^^^^^^^^^^^^^^^^')
root = tree._root
s = Solution()
CreateTree.display(s.convertBST(root))
예제 #3
0
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def averageOfLevels(self, root):
        """
        :type root: TreeNode
        :rtype: List[float]
        """


from Leetcode_easy.tree import CreateTree
lst = [2, 0 , 3, -4, 1]
tree = CreateTree.Create(lst)
CreateTree.breadth_tree(tree)
print('^^^^^^^^^^^^^^^^^^^^^')
root = tree._root
CreateTree.display_b(root)
s = Solution()
print(s.averageOfLevels(root))
예제 #4
0

class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        min = [99999]
        if not root:
            return 0

        def findmindepth(root, level):
            if root.left is None and root.right is None:
                if level < min[-1]:
                    min.append(level)
                return
            if root.left:
                findmindepth(root.left, level + 1)
            if root.right:
                findmindepth(root.right, level + 1)

        findmindepth(root, 1)
        return min[-1]


from Leetcode_easy.tree import CreateTree
lst = [1, 2, 3, 4, 5]
tree = CreateTree.Create(lst)
root = tree._root
예제 #5
0
        :rtype: TreeNode
        """
        if root is None:
            return None
        r = TreeNode(root.val)

        def Solution(root, r):
            if root.left is None and root.right is None:
                return
            if root.left:
                r.right = TreeNode(root.left.val)
                Solution(root.left, r.right)
            if root.right:
                r.left = TreeNode(root.right.val)
                Solution(root.right, r.left)

        Solution(root, r)
        return r


from Leetcode_easy.tree import CreateTree
lst = [1, 2, 3, 4, 5]
tree = CreateTree.Create(lst)
#CreateTree.breadth_tree(tree)
CreateTree.depth_tree(tree)
root = tree._root
s = Solution()
r2 = s.invertTree(root)
t2 = CreateTree.BinaryTree()
t2.make_tree(r2)
CreateTree.depth_tree(t2)