Skip to content

Commit 30c75ee

Browse files
committed
O(n) time and O(n) space using Deque(Iterative)
1 parent e9b069c commit 30c75ee

File tree

3 files changed

+27
-40
lines changed

3 files changed

+27
-40
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isSymmetric(self, root: TreeNode) -> bool:
9+
if not root:
10+
return True
11+
q = collections.deque()
12+
q.append(root)
13+
q.append(root)
14+
while q:
15+
node1 = q.popleft()
16+
node2 = q.popleft()
17+
if not node1 and not node2:
18+
continue
19+
if not node1 or not node2:
20+
return False
21+
if node1.val != node2.val:
22+
return False
23+
q.append(node1.left)
24+
q.append(node2.right)
25+
q.append(node1.right)
26+
q.append(node2.left)
27+
return True

101_Symmetric_Tree.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

101_Symmetric_Tree_1.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)