Esempio n. 1
0
            return self.isUnivalTree(root.left) and self.isUnivalTree(
                root.right)

    def isUnivalTree1(self, root):
        """
        栈
        :type root: TreeNode
        :rtype: bool
        """
        if not root:
            return None
        a_stack = [root]
        a_list = []
        while a_stack:
            node = a_stack.pop()
            if node.val not in a_list:
                a_list.append(node.val)
            if node.left:
                a_stack.append(node.left)
            if node.right:
                a_stack.append(node.right)
        return len(a_list) == 1


if __name__ == "__main__":
    # nodes = [1, 1, 1, 1, 1, None, 1]
    nodes = [9, 9, 9, 9, 9, 9, 6]
    root = create_tree(nodes)
    flag = Solution().isUnivalTree(root)
    print flag
Esempio n. 2
0
        :type root1: TreeNode
        :type root2: TreeNode
        :rtype: bool
        """
        a1 = self.get_child_arr(root1)
        a2 = self.get_child_arr(root2)
        return a1 == a2

    def get_child_arr(self, root):
        if not root:
            return []
        a_stack = [root]
        res = []
        while a_stack:
            p = a_stack.pop()
            if p.left:
                a_stack.append(p.left)
            if p.right:
                a_stack.append(p.right)
            if not (p.right or p.left):
                res.append(p.val)
        return res


if __name__ == "__main__":
    arr1 = [1, 2, 3, None, 5]
    tree1 = create_tree(arr1)
    # arr = Solution().leafSimilar(tree1)
    arr = Solution().get_child_arr(tree1)
    print arr
Esempio n. 3
0
            return t2
        if not t2:
            return t1
        t1.val += t2.val
        t1.left = self.mergeTrees(t1.left, t2.left)
        t1.right = self.mergeTrees(t1.right, t2.right)
        return t1

    def mergeTrees1(self, t1, t2):
        """
        不改变原树
        :type t1: TreeNode
        :type t2: TreeNode
        :rtype: TreeNode
        """
        if not t1 and not t2:
            return None
        root = TreeNode((0 if not t1.val else t1.val) +
                        (0 if not t2.val else t2.val))
        root.left = self.mergeTrees(t1.left, t2.left)
        root.right = self.mergeTrees(t1.right, t2.right)
        return root


if __name__ == "__main__":
    arr1 = [1, 3, 2, 5]
    arr2 = [2, 1, 3, None, 4, None, 7]
    tree1 = create_tree(arr1)
    tree2 = create_tree(arr2)
    tree = Solution().mergeTrees1(tree1, tree2)
Esempio n. 4
0
    def zigzagLevelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        res = []

        def inner(node, level):
            if not node:
                return []
            if len(res) <= level:
                res.append([])
            if level % 2 == 0:
                res[level].append(node.val)
            else:
                res[level].insert(0, node.val)
            if node.left:
                inner(node.left, level + 1)
            if node.right:
                inner(node.right, level + 1)

        inner(root, 0)
        return res


if __name__ == "__main__":
    arr = [3, 9, 20, None, None, 15, 7]
    root = create_tree(arr)
    res = Solution().zigzagLevelOrder(root)
    print res