← Tree Traversal

Micro-Drill #207 — Symmetric tree check

Tree Traversal Target: 10s

Mirror check: compare left.left with right.right AND left.right with right.left. Like 'same tree' but crossed.

def isSymmetric(root):
    def mirror(a, b):
        if not a and not b: return True
        if not a or not b: return False
        return (a.val == b.val and
                mirror(a.left, b.right) and
                mirror(a.right, b.left))
    return mirror(root.left, root.right)

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #206 Micro #208 →