Python Solution - output is not same as OJ


  • 0
    E

    I have my python solution like this:

    # Definition for a binary tree node.
    class TreeNode(object):
        def __init__(self, x):
            self.val = x
            self.left = None
            self.right = None
    
    class Solution(object):
        def Tree2List(self, root):
            lst = []
            lst.append(root.val)
            l = root.left
            r = root.right
    
            # append None to consider Tress structure
            if l is None:
                lst.append(None)
            if r is None:
                lst.append(None)
    
            if l is not None:
                lst += self.Tree2List(l)
    
            if r is not None:
                lst += self.Tree2List(r)
    
            return lst
    
        def isSameTree(self, p, q):
            """
            :type p: TreeNode
            :type q: TreeNode
            :rtype: bool
            """
            if p is None and q is None:
                return True
            if p is None or q is None:
                return False
    
            lst1 = self.Tree2List(p)
            lst2 = self.Tree2List(q)
    
            print lst1
            print lst2
            return lst1 == lst2
    
    
    
    def printTree(root):
        print root.val
        l = root.left
        r = root.right
    
        if l is not None:
            printTree(l)
    
        if r is not None:
            printTree(r)
    
    
    def main():
        t1 = TreeNode(1)
        t1.left = TreeNode(2)
        # printTree(t1)
    
        t2 = TreeNode(1)
        t2.left = TreeNode(None)
        t2.right = TreeNode(2)
        # printTree(t2)
    
    
        solution = Solution()
        print solution.isSameTree(t1, t2)
    
    
    if __name__ == '__main__':
        main()
    

    With test case

    [1, 2]
    [1, null, 2]
    

    according to

    print lst1
    print lst2
    

    I got this in my local machine.
    lst1 = [1, None, 2, None, None]
    lst2 = [1, None, None, None, 2, None, None]

    However, I got below output in OJ, which is different from my local machine.
    lst1 = [1, None, 2, None, None]
    lst2 = [1, None, 2, None, None]

    Any one knows why??

    python version in local machine:
    Python 2.7.9 (default, May 1 2015, 19:04:44)
    [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)] on darwin


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.