예제 #1
0
    def addOneRow(self, root, v, d):
        """
        :type root: TreeNode
        :type v: int
        :type d: int
        :rtype: TreeNode
        """
        def helper(root, v, d, level):
            if not root:
                return
            if level + 1 == d:
                node_left = TreeNode(v)
                node_left.left = root.left
                root.left = node_left
                node_right = TreeNode(v)
                node_right.right = root.right
                root.right = node_right
                return
            helper(root.left, v, d, level + 1)
            helper(root.right, v, d, level + 1)

        if d == 1:
            node = TreeNode(v)
            node.left = root
            return node
        helper(root, v, d, 1)
        return root
 def reConstructBinaryTree(self, pre, tin):
     if len(pre) == 0:
         return
     root = TreeNode(pre[0])
     tinIndex = tin.index(pre[0])
     root.left = self.reConstructBinaryTree(pre[1:tinIndex + 1],
                                            tin[:tinIndex])
     root.right = self.reConstructBinaryTree(pre[tinIndex + 1:],
                                             tin[tinIndex + 1:])
     return root
예제 #3
0
 def dfs(self, left, right):
     res = []
     if left > right:
         return [None]
     for i in range(left, right+1):
         left_nodes = self.dfs(left, i-1)
         right_nodes = self.dfs(i+1, right)
         for left_node in left_nodes:
             for right_node in right_nodes:
                 node = TreeNode(i)
                 node.left = left_node
                 node.right = right_node
                 res.append(node)
     return res
예제 #4
0
 def buildTree(self, inorder, postorder):
     """
     :type inorder: List[int]
     :type postorder: List[int]
     :rtype: TreeNode
     """
     if not postorder:
         return None
     root = TreeNode(postorder[-1])  # 后序中最后肯定是根结点
     root_index = inorder.index(root.val)
     root.left = self.buildTree(inorder[:root_index],
                                postorder[:root_index])  #左闭右开
     root.right = self.buildTree(inorder[root_index + 1:],
                                 postorder[root_index:-1])
     return root
예제 #5
0
 def helper(root, v, d, level):
     if not root:
         return
     if level + 1 == d:
         node_left = TreeNode(v)
         node_left.left = root.left
         root.left = node_left
         node_right = TreeNode(v)
         node_right.right = root.right
         root.right = node_right
         return
     helper(root.left, v, d, level + 1)
     helper(root.right, v, d, level + 1)
        if not root:
            return 0
        return max(self.maxDepth1(root.left), self.maxDepth1(root.right)) + 1

    def maxDepth2(self, root):
        '''
        BFS
        '''
        if not root:
            return 0
        que = []
        que.append((root, 1))
        while que:
            cur, depth = que.pop(0)
            if cur.left != None: que.append((cur.left, depth + 1))
            if cur.right != None: que.append((cur.right, depth + 1))
        return depth

    def maxDepth(self, root):
        return self.maxDepth2(root)


if __name__ == '__main__':
    s = Solution()
    root = TreeNode(3)
    root.left = TreeNode(9)
    root.right = TreeNode(20)
    root.right.left = TreeNode(15)
    root.right.right = TreeNode(7)
    print(s.maxDepth(root))
        res = []
        self.getPath(root, sum, [], res)
        return res

    def getPath(self, root, sums, tmp, res):
        tmp.append(root.val)
        if (root.left == None) and (root.right == None):
            if sum(tmp) == sums:
                res.append(tmp)
            return
        if root.left != None:
            self.getPath(root.left, sums, tmp[:], res)  # 传入的tmp是快照
        if root.right != None:
            self.getPath(root.right, sums, tmp[:], res)


if __name__ == '__main__':
    s = Solution()
    root = TreeNode(5)
    root.left = TreeNode(4)
    root.left.left = TreeNode(11)
    root.left.left.left = TreeNode(7)
    root.left.left.right = TreeNode(2)
    root.right = TreeNode(8)
    root.right.left = TreeNode(13)
    root.right.right = TreeNode(4)
    root.right.right.left = TreeNode(5)
    root.right.right.right = TreeNode(1)
    print(s.pathSum(root, 22))
예제 #8
0
        :rtype: List[int]
        """
        res = []
        if not root:
            return res

        stack = []
        stack.append(root)
        while stack != []:
            tmp = stack[-1]
            res.append(tmp.val)         # 取根节点的值
            stack.pop()
            if tmp.right:
                stack.append(tmp.right) # 先压右节点
            if tmp.left:
                stack.append(tmp.left)  # 再压左节点
        return res

    def preorderTraversal(self, root):
        return self.preorderTraversal1(root)

if __name__ == '__main__':
    s = Solution()
    root = TreeNode(3)
    root.left = TreeNode(1)
    root.right = TreeNode(2)
    print(s.preorderTraversal(root))
    # stack = []
    # stack.append('3')
    # tmp = stack[0]
    # print(tmp)
                return 0
            if take:
                if root.left not in storage_0:
                    storage_0[root.left] = helper(root.left, 0)
                if root.right not in storage_0:
                    storage_0[root.right] = helper(root.right, 0)
                return root.val + storage_0[root.left] + storage_0[root.right]
            else:
                if root.left not in storage_0:
                    storage_0[root.left] = helper(root.left, 0)
                if root.right not in storage_0:
                    storage_0[root.right] = helper(root.right, 0)
                if root.left not in storage_1:
                    storage_1[root.left] = helper(root.left, 1)
                if root.right not in storage_1:
                    storage_1[root.right] = helper(root.right, 1)
                return max(storage_0[root.left], storage_1[root.left]) + max(
                    storage_0[root.right], storage_1[root.right])

        return max(helper(root, 0), helper(root, 1))


if __name__ == '__main__':
    s = Solution()
    root = TreeNode(3)
    root.left = TreeNode(4)
    root.left.left = TreeNode(1)
    root.left.right = TreeNode(3)
    root.right = TreeNode(5)
    root.right.right = TreeNode(1)
    print(s.rob(root))
        res = []
        if not root:
            return res
        map = {}

        def helper(root):
            if not root:
                return 0
            tmp = root.val + helper(root.left) + helper(root.right)
            map[tmp] = map.get(tmp, 0) + 1
            return tmp

        helper(root)
        max_sum = 0
        for k, v in map.items():
            if v > max_sum:
                max_sum = v
                res.clear()  # or res[:] = [],python3.3后才有.clear()
                res.append(k)
            elif v == max_sum:
                res.append(k)
        return res


if __name__ == '__main__':
    root = TreeNode(5)
    root.left = TreeNode(2)
    root.right = TreeNode(-5)
    s = Solution()
    print(s.findFrequentTreeSum(root))
        if not res:
            res = self.HasSubtree(pRoot1.left, pRoot2)  # A树的左节点中有没有
        if not res:
            res = self.HasSubtree(pRoot1.right, pRoot2) # A树的右节点中有没有
        return res

    def isSubtree(self, root1, root2):
        if not root2:
            return True
        if not root1:
            return False
        if root1.val == root2.val:
            return self.isSubtree(root1.left, root2.left) and self.isSubtree(root1.right, root2.right)
        else:
            return False


if __name__ == '__main__':
    s = Solution()
    a = TreeNode(8)
    a.left = TreeNode(8)
    a.left.left = TreeNode(9)
    a.left.left.left = TreeNode(9)
    a.left.right = TreeNode(2)
    a.right = TreeNode(7)
    b = TreeNode(8)
    b.left = TreeNode(9)
    b.right = TreeNode(2)
    print(s.HasSubtree(a, b))

        tmp = root.left
        root.left = root.right
        root.right = tmp
        self.Mirror(root.left)
        self.Mirror(root.right)


def OutputTreeFront(root):
    """
    先序遍历:根结点 ---> 左子树 ---> 右子树
    """
    if root == None:
        return
    print(root.val)
    OutputTreeFront(root.left)
    OutputTreeFront(root.right)


if __name__ == '__main__':
    s = Solution()
    root = TreeNode(8)
    root.left = TreeNode(6)
    root.right = TreeNode(10)
    # root.left.left = TreeNode(5)
    root.left.right = TreeNode(7)
    root.right.left = TreeNode(9)
    root.right.right = TreeNode(11)

    s.Mirror(root)
    OutputTreeFront(root)
예제 #13
0
从上往下打印二叉树
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
'''
from tool import TreeNode


class Solution:
    # 返回从上到下每个节点值列表,例:[1,2,3]
    def PrintFromTopToBottom(self, root):
        if not root:
            return []
        res = []
        que = [root]
        while que:
            cur = que.pop(0)
            res.append(cur.val)
            if cur.left: que.append(cur.left)
            if cur.right: que.append(cur.right)
        return res


if __name__ == '__main__':
    s = Solution()
    root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    root.left.left = TreeNode(4)
    root.right.left = TreeNode(6)
    root.right.right = TreeNode(7)
    print(s.PrintFromTopToBottom(root))