Side Views, Two Trees

Side Views

545. Boundary of Binary Tree (Medium)

def boundaryOfBinaryTree(self, root: Optional[TreeNode]) -> List[int]:
    def setLeaves(node):
        nonlocal boundary
        if node:
            if not node.left and not node.right:
                boundary.append(node.val)
            setLeaves(node.left)
            setLeaves(node.right)

    def setLeftBoundary(node):
        if node:
            if not node.left and not node.right:
                return
            boundary.append(node.val)
            setLeftBoundary(node.left)
            setLeftBoundary(node.right) if not node.left else None

    def setRightBoundary(node):
        if node:
            if not node.left and not node.right:
                return
            setRightBoundary(node.right)
            setRightBoundary(node.left) if not node.right else None
            boundary.append(node.val)

    boundary = [root.val]
    setLeftBoundary(root.left) if root.left else None
    setLeaves(root) if root.left or root.right else None
    setRightBoundary(root.right) if root.right else None
    return bounda

199. Binary Tree Right Side View (Medium)

def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
    def recursion(node, level):
        nonlocal hm
        if node:
            hm[level] = node.val
            recursion(node.left, level + 1)
            recursion(node.right, level + 1)

    hm = {}
    recursion(root, 1)
    return list(hm.values())

Two Trees

100. Same Tree (Easy)

def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
    def recursion(a, b):
        if a and b and a.val == b.val:
            x = recursion(a.left, b.left)
            y = recursion(a.right, b.right)
            return x and y
        elif not a and not b:
            return True
        return False

    return recursion(p, q)

101. Symmetric Tree (Easy)

def isSymmetric(self, root: Optional[TreeNode]) -> bool:
    def recursion(left, right):
        if left and right and left.val == right.val:
            a = recursion(left.right, right.left)
            b = recursion(left.left, right.right)
            return a and b
        elif not left and not right:
            return True
        return False

    return recursion(root.left, root.right)

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree (Easy)

def getTargetCopy(
    self, original: TreeNode, cloned: TreeNode, target: TreeNode
) -> TreeNode:
    def recursion(a, b):
        if a and b:
            if a == target:
                return b
            return recursion(a.left, b.left) or recursion(a.right, b.right)

    return recursion(original, cloned)

872. Leaf-Similar Trees (Easy)

def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
    def recursion(node):
        if not node:
            return
        if not node.left and not node.right:
            yield node.val
        yield from recursion(node.left)
        yield from recursion(node.right)

    return list(recursion(root1)) == list(recursion(root2))

617. Merge Two Binary Trees (Easy)

def mergeTrees(
    self, root1: Optional[TreeNode], root2: Optional[TreeNode]
) -> Optional[TreeNode]:
    def recursion(a, b):
        if a and b:
            node = TreeNode(a.val + b.val)
            node.left = recursion(a.left, b.left)
            node.right = recursion(a.right, b.right)
        elif a:
            node = TreeNode(a.val)
            node.left = recursion(a.left, None)
            node.right = recursion(a.right, None)
        elif b:
            node = TreeNode(b.val)
            node.left = recursion(None, b.left)
            node.right = recursion(None, b.right)
        else:
            return None
        return node

    return recursion(root1, root2)