#!/usr/bin/env python3

"""
http://oj.leetcode.com/problems/same-tree/

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and
the nodes have the same value.

Wed Apr  9 16:40:26 CST 2014
"""
# Definition for a binary tree node


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    # @param p, a tree node
    # @param q, a tree node
    # @return a boolean
    def isSameTree(self, p, q):
        if not p or not q:
            return p == q
        elif p.val != q.val:
            return False
        else:
            return (self.isSameTree(p.left, q.left) and
                    self.isSameTree(p.right, q.right))

@@ -799,8 +799,1817 @@
 right))%0A
+%0A    def isSameTree1(self, p, q):%0A        %22%22%22 A version from an interviewee, too long but right.%0A        %22%22%22%0A        if p is None or q is None:%0A            if p is None and q is None:%0A                return True%0A            else:%0A                return False%0A        else:%0A            if p.left and q.left:%0A                l = self.isSameTree1(p.left, q.left)%0A            elif p.left is None and q.left is None:%0A                l = True%0A            else:%0A                l = False%0A%0A            if p.right and q.right:%0A                r = self.isSameTree1(p.right, q.right)%0A            elif p.right is None and q.right is None:%0A                r = True%0A            else:%0A                r = False%0A%0A            if not (l and r):%0A                return False%0A            elif p.val == q.val:%0A                return True%0A            else:%0A                return False%0A%0A%0Aif __name__ == '__main__':%0A    # These test cases guarantee a 100%25 coverage.%0A    s = Solution()%0A%0A    p = q = None%0A    assert s.isSameTree(p, q)%0A    assert s.isSameTree1(p, q)%0A%0A    p = None%0A    q = TreeNode(1)%0A    assert not s.isSameTree(p, q)%0A    assert not s.isSameTree1(p, q)%0A%0A    p = TreeNode(1)%0A    q = None%0A    assert not s.isSameTree(p, q)%0A    assert not s.isSameTree1(p, q)%0A%0A    n1 = TreeNode(1)%0A    n2 = TreeNode(2)%0A    n3 = TreeNode(3)%0A    n1.left = n2%0A    n1.right = n3%0A    n4 = TreeNode(1)%0A    n5 = TreeNode(2)%0A    n6 = TreeNode(3)%0A    n4.left = n5%0A    n4.right = n6%0A    p = n1%0A    q = n4%0A    assert s.isSameTree(p, q)%0A    assert s.isSameTree1(p, q)%0A%0A    p = n1%0A    q = n2%0A    assert not s.isSameTree(p, q)%0A    assert not s.isSameTree1(p, q)%0A%0A    p = n1%0A    n7 = TreeNode(4)%0A    n8 = TreeNode(5)%0A    n9 = TreeNode(6)%0A    n7.left = n8%0A    n7.right = n9%0A    q = n7%0A    assert not s.isSameTree(p, q)%0A    assert not s.isSameTree1(p, q)%0A

Add another version and test cases.