1
+ # Given the root of a binary tree, check whether it is a mirror of itself
2
+ # (i.e., symmetric around its center).
3
+
4
+
5
+ # Example 1:
6
+ # Input: root = [1,2,2,3,4,4,3]
7
+ # Output: true
8
+
9
+ # Example 2:
10
+ # Input: root = [1,2,2,null,3,null,3]
11
+ # Output: false
12
+
13
+
14
+ # Constraints:
15
+ # The number of nodes in the tree is in the range [1, 1000].
16
+ # -100 <= Node.val <= 100
17
+
18
+
19
+ from typing import Optional
20
+ # Definition for a binary tree node.
21
+ class TreeNode :
22
+ def __init__ (self , val = 0 , left = None , right = None ):
23
+ self .val = val
24
+ self .left = left
25
+ self .right = right
26
+ class Solution :
27
+ def isSymmetric (self , root : Optional [TreeNode ]) -> bool :
28
+ def symmetric (root1 , root2 ):
29
+ if (not root1 ) and (not root2 ):
30
+ return True
31
+ if (not root1 ) or (not root2 ):
32
+ return False
33
+ if root1 .val != root2 .val :
34
+ return False
35
+ return (symmetric (root1 .left , root2 .right ) and symmetric (root1 .right , root2 .left ))
36
+
37
+ return symmetric (root , root )
0 commit comments