My iterative solution for your reference


  • 0
    U
    # Definition for a  binary tree node
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        # @param root, a tree node
        # @return a boolean
        def isSymmetric(self, root):
            
            def symmetric(node_list):  # check if a list of TreeNode is symmetric
                for i in range(len(node_list)//2):
                    if node_list[i]==None:
                        if node_list[-1-i]!=None: return False
                    else:
                        if node_list[-1-i]==None: 
                            return False
                        else:
                            if node_list[i].val!=node_list[-1-i].val: return False
                return True
                
            p_list=[root]
            while True:
                c_list=[]
                for p_node in p_list:
                    if p_node!=None:
                        c_list.append(p_node.left)
                        c_list.append(p_node.right)
                if not c_list: return True  #c_list is empty, namely, p_list is all nones
                if not symmetric(c_list): return False
                p_list=c_list

Log in to reply
 

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