Example #1
0
 def recursion(self, root, left, right, inorderDict, preorder):
     # step 1 : 判定 是否 到达 叶子节点
     if left > right:
         return
     # step 2 : 构建根节点
     node = Tree(preorder[root])
     # step 3 : 搜索 根节点 在 中序遍历 中 的位置
     index = inorderDict[preorder[root]]
     # step 4 : 分割 左右子树
     node.left = self.recursion(root + 1, left, index - 1, inorderDict,
                                preorder)
     node.right = self.recursion(index - left + root + 1, index + 1, right,
                                 inorderDict, preorder)
     return node
Example #2
0
 def left_right(left, right):
     if left > right:
         return [None]
     if (left, right) in dct:
         return dct[(left, right)]
     ret = []
     for i in range(left, right + 1):
         left_lst = left_right(left, i - 1)
         right_lst = left_right(i + 1, right)
         for L in left_lst:
             for R in right_lst:
                 app_Tree = Tree(i)
                 app_Tree.left = L
                 app_Tree.right = R
                 ret.append(app_Tree)
     dct[(left, right)] = ret
     return ret
Example #3
0
                res.append("->".join(path))
                path.pop()
                return
            if root.left:
                backtrack(root.left, res, path)

            if root.right:
                backtrack(root.right, res, path)
            path.pop()

        backtrack(root, res, [])
        return res


if __name__ == "__main__":

    tree = Tree()
    solution = Solution()
    while 1:
        str1 = input()
        if str1 != "":
            node_list = [num for num in str1.split(",")]
            for node in node_list:
                tree.add(node)
            print(f"层次遍历:{tree.traverse(tree.root)}")

            res = solution.binaryTreePaths(tree.root)
            print(res)
        else:
            break
Example #4
0
                    totals[level] = totals[level]+root.val
                    counts[level] = counts[level]+1
                else:
                    totals.append(root.val)
                    counts.append(1)
                dfs(root.left,level+1)
                dfs(root.right,level+1)

        counts = []
        totals = []
        dfs(root,0)
        return [total / count for total, count in zip(totals, counts)]
        
if __name__ == "__main__":
    
    tree1 = Tree()
    tree2 = Tree()
    solution = Solution()
    while 1:
        str1 = input()
        if str1 != "":
            node_list1 = str1.split(",")
            for node in node_list1:
                tree1.add(node)

            print(f"层次遍历:{tree1.traverse(tree1.root)}")

            res = solution.averageOfLevels1(tree1.root)
            print(res)
        else:
            break
Example #5
0
class Solution:
    def dfs(self, root):
        if not root:
            return [0, 0]
        l = self.dfs(root.left)
        r = self.dfs(root.right)
        return [max(l) + max(r), root.val + l[0] + r[0]]

    def rob(self, root):
        return max(self.dfs(root))


if __name__ == "__main__":

    tree = Tree()
    solution = Solution()
    while 1:
        str1 = input()
        if str1 != "":
            node_list = str1.split(",")
            for node in node_list:
                tree.add(int(node))

            print(f"层次遍历:{tree.traverse(tree.root)}")

            res = solution.rob(tree.root)
            print(res)
        else:
            break
Example #6
0
    def recursion(self, root, left, right, inorderDict, preorder):
        # step 1 : 判定 是否 到达 叶子节点
        if left > right:
            return
        # step 2 : 构建根节点
        node = Tree(preorder[root])
        # step 3 : 搜索 根节点 在 中序遍历 中 的位置
        index = inorderDict[preorder[root]]
        # step 4 : 分割 左右子树
        node.left = self.recursion(root + 1, left, index - 1, inorderDict,
                                   preorder)
        node.right = self.recursion(index - left + root + 1, index + 1, right,
                                    inorderDict, preorder)
        return node


if __name__ == "__main__":
    tree1 = Tree()
    tree2 = Tree()
    solution = Solution()
    while 1:
        str1 = input()
        str2 = input()
        if str1 != "" and str2 != "":
            preorder = str1.split(",")
            inorder = str2.split(",")
            res = solution.buildTree(preorder, inorder)
            print(res)
        else:
            break
Example #7
0
            # 判断当前节点 是不是 为 p 或 q 中的一个
            mid = current_node==p or current_node==q

            # 若 mid、left、right 三个中 有两个 为 True,那么表示找到 最近父节点
            if mid+left+right>=2:
                self.res = current_node
            
            return mid or left or right

        recurse_tree(root)
        return self.res
             
        
if __name__ == "__main__":
    
    tree = Tree()
    tree_p = Tree()
    tree_q = Tree()
    solution = Solution()
    while 1:
        str1 = input()
        str2 = input()
        str3 = input()
        if str1 != "" and str2 != "" and str3 != "":
            node_list = str1.split(",")
            for node in node_list:
                tree.add(node)
            print(f"层次遍历:{tree.traverse(tree.root)}")

            p = tree.select(tree.root,int(str2))
            q = tree.select(tree.root,int(str3))
Example #8
0
    def check(self, s, t):
        if not s and not t:
            return True
        if (not s and t) or (s and not t) or (s.val != t.val):
            return False
        return self.check(s.left, t.left) and self.check(s.right, t.right)


if __name__ == "__main__":

    solution = Solution()
    while 1:
        str1 = input()
        str2 = input()
        if str1 != "" and str2 != "":
            s_tree = Tree()
            t_tree = Tree()
            s_node_list = [int(num) for num in str1.split(",")]
            for node in s_node_list:
                s_tree.add(node)
            t_node_list = [int(num) for num in str2.split(",")]
            for node in t_node_list:
                t_tree.add(node)
            print(f"层次遍历:{s_tree.traverse(s_tree.root)}")
            print(f"层次遍历:{t_tree.traverse(t_tree.root)}")

            res = solution.isSubtree(s_tree.root, t_tree.root)
            print(res)
        else:
            break