示例#1
0
    #         self.val = val
    #         self.left = left
    #         self.right = right
    def findSecondMinimumValue(self, root: TreeNode) -> int:
        def two_min(node: TreeNode) -> Tuple[int, int]:
            buf = []
            heapq.heapify(buf)
            heapq.heappush(buf, node.val)
            if node.left:
                a, b = two_min(node.left)
                if a >= 0 and a not in buf:
                    heapq.heappush(buf, a)
                if b >= 0 and b not in buf:
                    heapq.heappush(buf, b)
            if node.right:
                a, b = two_min(node.right)
                if a >= 0 and a not in buf:
                    heapq.heappush(buf, a)
                if b >= 0 and b not in buf:
                    heapq.heappush(buf, b)
            min1st = heapq.heappop(buf) if len(buf) > 0 else -1
            min2nd = heapq.heappop(buf) if len(buf) > 0 else -1
            return min1st, min2nd

        return two_min(root)[1]


ans = Solution().findSecondMinimumValue(
    TreeNode.from_str("[2,2,5,null,null,5,7]"))
print(ans)
示例#2
0
        if not (root.left and root.right):
            return False

        queue = deque()
        queue.append(root.left)
        queue.append(root.right)
        while queue:
            tmp = []
            for _ in range(len(queue)):
                tmp.append(queue.popleft())
            i = 0
            j = len(tmp) - 1
            flag = True
            while i < j:
                flag = flag and is_node_sym(tmp[i], tmp[j])
                i += 1
                j -= 1
            if not flag:
                return False
            for node in tmp:
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        return True


tree = TreeNode.from_str("[1,2,2,null,3,3]")
print(TreeNode.to_str(tree))
ans = Solution().isSymmetric(tree)
print(ans)