-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101.py
More file actions
22 lines (19 loc) · 682 Bytes
/
101.py
File metadata and controls
22 lines (19 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if not root:
return True
return self._is_symmetric(root.left, root.right)
def _is_symmetric(self, node1: TreeNode, node2: TreeNode) -> bool:
if not node1 and not node2:
return True
if not node1 or not node2:
return False
if node1.val != node2.val:
return False
return self._is_symmetric(node1.left, node2.right) and self._is_symmetric(node1.right, node2.left)