Untitled
unknown
python
a year ago
1.1 kB
6
Indexable
from collections import deque # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: # use a queue, traverse the tree left to right and check every time it's traversed if not root: return True q = [] # q = [] q.append(root.left) # [2] q.append(root.right) # [2, 2] # bfs = stack # dfs = queue # q = [2, 2], left = 2, right = 2 while q: left = q.pop(0) # 2 right = q.pop(0) # 2 if not left and not right: continue if (not left and right) or (not right and left): return False if left.val != right.val: return False # add outside mirror q.append(left.left) q.append(right.right) # and inside mirror q.append(left.right) q.append(right.left) return True
Editor is loading...
Leave a Comment