-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101_Symmetric_Tree.py
More file actions
40 lines (29 loc) · 875 Bytes
/
101_Symmetric_Tree.py
File metadata and controls
40 lines (29 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
"""
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
import collections.deque
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if root is None:
return True
return self.isChildrenSymmetric(root.left, root.right)
def isChildrenSymmetric(self, left, right):
if left == right == None:
return True
elif left == None or right == None:
return False
elif left.val == right.val:
return self.isChildrenSymmetric(left.left, right.right) and self.isChildrenSymmetric(left.right, right.left)
else:
return False