Esempio n. 1
0
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
        if not p and not q:
            return True
        # if (not p and q) or (p and not q):
        if not p or not q:
            return False
        if p.val != q.val:
            return False

        return self.isSameTree(p.left, q.left) and self.isSameTree(
            p.right, q.right)


if __name__ == '__main__':
    slt = Solution()
    treeA1 = treeB1 = build2([1, 2, 3])
    treeA2, treeB2 = build2([1, 2]), build2([1, None, 2])
    treeA3, treeB3 = build2([1, 2, 1]), build2([1, 1, 2])

    print(slt.isSameTree(treeA1, treeB1))
    print(slt.isSameTree(treeA2, treeB2))
    print(slt.isSameTree(treeA3, treeB3))
class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        def helper(root: Optional[TreeNode], sum: int = 0) -> bool:

            if not root:
                return False

            sum += root.val

            if not root.left and not root.right:
                return sum == targetSum

            return helper(root.left, sum) or helper(root.right, sum)

        return helper(root)


if __name__ == '__main__':
    slt = Solution()
    forest = []

    forest.append((build2([1, 2]), 1))
    forest.append((build2([1, 2, 3]), 5))
    forest.append(
        (build2([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1]), 22))
    forest.append((build2([]), 0))

    for tree, sum in forest:
        # print(tree)
        print(slt.hasPathSum(tree, sum))
                    queue.append(node.right)
            level += 1

        return level

    def maxDepth_interative(self, root: Optional[TreeNode]) -> int:
        stack = [[root, 1]]
        result = 0
        while stack:
            node, depth = stack.pop()

            if node:
                result = max(result, depth)
                stack.append([node.right, depth + 1])
                stack.append([node.left, depth + 1])

        return result


if __name__ == '__main__':
    slt = Solution()
    tree1 = build2([3, 9, 20, None, None, 15, 7])
    tree2 = build2([1, None, 2])

    print(slt.maxDepth(tree1))
    print(slt.maxDepth(tree2))
    print(slt.maxDepth_bfs(tree1))
    print(slt.maxDepth_bfs(tree2))
    print(slt.maxDepth_interative(tree1))
    print(slt.maxDepth_interative(tree2))
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def preorderTraversal(self, root: TreeNode) -> list[int]:
        result = []
        if not root:
            return []

        def helper(node):
            if node:
                result.append(node.val)
                helper(node.left)
                helper(node.right)
            return result

        helper(root)
        return result


import binarytree
my_tree = binarytree.build2([1, None, 2])
# values = [1, None, 2, 3]
# root = bt.build2(values)
solution = Solution()
print(solution.preorderTraversal(my_tree))
            result.append(root.val)
            inorder(root.right)

        inorder(root)
        return result

    def inorderTraversal2(self, root: Optional[TreeNode]) -> List[int]:
        result: List[int] = []
        stack = []
        current = root

        while current or stack:
            while current:
                stack.append(current)
                current = current.left
            current = stack.pop()
            result.append(current.val)
            current = current.right

        return result


if __name__ == '__main__':
    slt = Solution()
    # nodes = [1, None, 2, 3]
    nodes = [1, 2, 3, 4, 5, 6, 7]

    binary_tree_1 = build2(nodes)
    print(binary_tree_1)
    print(slt.inorderTraversal2(binary_tree_1))
Esempio n. 6
0
        self.left = left
        self.right = right


class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        def helper(root: Optional[TreeNode]) -> (bool, int):
            if not root:
                return True, 0

            left = helper(root.left)
            right = helper(root.right)
            is_balanced = abs(left[1] - right[1]) <= 1 and (left[0] and right[0])

            return is_balanced, 1 + max(left[1], right[1])

        return helper(root)[0]

if __name__ == '__main__':
    slt = Solution()

    tree1 = build2([3, 9, 20, None, None, 15, 7])
    tree2 = build2([1, 2, -2, 3, 3, None, None, 4, 4])
    tree3 = build2([1, 2, 3, 4, 5, None, 6, 7, None, None, None, None, 8])

    # print(tree3)

    print(slt.isBalanced(tree1))
    print(slt.isBalanced(tree2))
    print(slt.isBalanced(tree3))
Esempio n. 7
0
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        def helper(left: Optional[TreeNode],
                   right: Optional[TreeNode]) -> bool:
            if not left and not right:
                return True
            if not left or not right:
                return False
            if left.val != right.val:
                return False

            return helper(left.left, right.right) and helper(
                left.right, right.left)

        return helper(root.left, root.right)


if __name__ == '__main__':
    slt = Solution()

    tree1 = build2([1, 2, 2, 3, 4, 4, 3])
    tree2 = build2([1, 2, 2, None, 3, None, 3])

    print(slt.isSymmetric(tree1))
    print(slt.isSymmetric(tree2))
Constraints:
The number of nodes in the tree is in the range [0, 105].
-1000 <= Node.val <= 1000
'''
from typing import Optional
from binarytree import build2, Node

TreeNode = Node


class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        if not root.left or not root.right:
            return 1 + max(self.minDepth(root.left), self.minDepth(root.right))

        return 1 + min(self.minDepth(root.left), self.minDepth(root.right))


if __name__ == '__main__':
    slt = Solution()
    forest = []
    forest.append(build2([3, 9, 20, None, None, 15, 7]))
    forest.append(build2([2, None, 3, None, 4, None, 5, None, 6]))

    for tree in forest:
        print(tree)
        print(slt.minDepth(tree))