My solution for symmetric tree is:

Inorder traverse the binary tree, so for the below tree, I get '3241423'
1 / \ 2 2 /\ /\ 3 4 4 3
Then simply check if the this string equals to its reverse (i.e., palindrome)
Below is my python code,
class Solution:
x=''
# @param root, a tree node
# @return a boolean
def isSymmetric(self, root):
if root is None:
return True
self.inorder(root)
return self.x == self.x[::1]
def inorder(self, node):
if node is None:
return
self.inorder(node.left)
self.x += str(node.val)
self.inorder(node.right)
which is however rejected for wrong answer: {1,2} outputs True.
Why?
p.s. When I tried to debug, I don't really know how LeetCode builds the tree in the first place (i.e., the input parameter root
). Can someone also suggest how to construct the input root
?