Skip to content

Commit 825aecb

Browse files
authored
Create 101_Symmetric_Tree.md
1 parent e01bed1 commit 825aecb

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

101_Symmetric_Tree.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## 101. Symmetric Tree
2+
3+
```
4+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
5+
6+
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
7+
8+
1
9+
/ \
10+
2 2
11+
/ \ / \
12+
3 4 4 3
13+
14+
15+
But the following [1,2,2,null,3,null,3] is not:
16+
17+
1
18+
/ \
19+
2 2
20+
\ \
21+
3 3
22+
23+
24+
Follow up: Solve it both recursively and iteratively.
25+
```
26+
27+
```python
28+
def isSymmetric(self, root: TreeNode) -> bool:
29+
if not root: return True
30+
return self.isMirror(root.left, root.right)
31+
32+
def isMirror(self, left, right):
33+
if not left and not right: return True
34+
if not left or not right: return False
35+
if left.val == right.val:
36+
return self.isMirror(left.left, right.right) and self.isMirror(left.right, right.left)
37+
else:
38+
return False
39+
40+
```
41+
42+
```
43+
Runtime: 32 ms, faster than 81.08% of Python3 online submissions for Symmetric Tree.
44+
Memory Usage: 14.1 MB, less than 100.00% of Python3 online submissions for Symmetric Tree.
45+
```

0 commit comments

Comments
 (0)