Saturday, August 01, 2015

is binary tree symmetric

bool is-symmetric(node):
    if node == null:
        throw
    return is-symmetric(node.left, node.right)

bool is-symmetric(n1, n2):
    if n1 == null && n2 == null:
        return true
    if n1 == null || n2 == null:
        return false
    return n1.value == n2.value
        && is-symmetric(n1.left, n2.right)
        && is-symmetric(n1.right, n2.left)

No comments:

Post a Comment