#error caused by:
#1: {} did not consider the none input
#2: {1,2,2} does not control H and t
#3: {4,-57,-57,#,67,67,#,#,-97,-97} does not consider the minus sign, the s from STR to list, using 9999 for empty values;
---------------------
The symmetry is verified by layer, and the asymmetry is ended;
1 #Definition for a binary tree node2 #class TreeNode:3 #def __init__ (self, x):4 #self.val = x5 #self.left = None6 #self.right = None7 8 classSolution:9 #@param root, a tree nodeTen #@return A Boolean One defissymmetric (self,root): ARET =True -Q = [] -s = [] the ifRoot! = None:#Note root is empty - q.append (Root) - s.append (root.val) - while(Len (q)! = 0 andRET = =True): +h =0 -t = Len (s)-1 + while(H <t): A if(S[h]! =s[t]): atRET =False - Break -H+=1#I forgot to control H and t here. -T-=1 -TQ =Q -s = [] inQ = [] - while(Len (TQ)! = 0 andRET = =True): tot = Tq.pop (0)#Pop pops up last value by default + ifT.left! =None: - q.append (t.left) the s.append (t.left.val) * Else: $S.append (9999)Panax Notoginseng ifT.right! =None: - q.append (t.right) the s.append (t.right.val) + Else: AS.append (9999) the returnRet
Leetcode:symmetric Tree "Python Edition"