Skip to content

Commit 765ea1c

Browse files
authored
Merge pull request gzc426#304 from Scottnan/master
Create Despacito.md
2 parents 17cd4c9 + 5c3e125 commit 765ea1c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

2018.12.04-leetcode101/Despacito.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# leetcode 101 Symmetric Tree
2+
## 1. Description
3+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
4+
5+
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
6+
7+
1
8+
/ \
9+
2 2
10+
/ \ / \
11+
3 4 4 3
12+
13+
But the following [1,2,2,null,3,null,3] is not:
14+
15+
1
16+
/ \
17+
2 2
18+
\ \
19+
3 3
20+
21+
**Note:**
22+
23+
Bonus points if you could solve it both recursively and iteratively.
24+
25+
## 2. Solution
26+
```python
27+
# Definition for a binary tree node.
28+
# class TreeNode:
29+
# def __init__(self, x):
30+
# self.val = x
31+
# self.left = None
32+
# self.right = None
33+
34+
class Solution:
35+
def isSymmetric(self, root):
36+
"""
37+
:type root: TreeNode
38+
:rtype: bool
39+
"""
40+
if root == None:
41+
return True
42+
return self.comroot(root.left, root.right)
43+
44+
def comroot(self, left, right):
45+
if left == None and right == None:
46+
return True
47+
elif left == None or right == None:
48+
return False
49+
elif left.val != right.val:
50+
return False
51+
return self.comroot(left.left, right.right) and self.comroot(left.right, right.left)
52+
```

0 commit comments

Comments
 (0)