forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymmetric_Tree.py
More file actions
65 lines (58 loc) · 1.73 KB
/
Symmetric_Tree.py
File metadata and controls
65 lines (58 loc) · 1.73 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
"""
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param root, a tree node
# @return a boolean
def isSymmetric(self, root):
return self.isSymmetric_2(root)
def isSymmetric_1(self, root):
if root is None:
return True
return self.symmetric_helper(root.left, root.right)
def symmetric_helper(self, n1, n2):
if not n1 and not n2:
return True
if not n1 or not n2 or n1.val != n2.val:
return False
return self.symmetric_helper(n1.left, n2.right) and self.symmetric_helper(n1.right, n2.left)
# No need to use two queues here, just one but pop twice would be fine
# Keep in mind which node should be pop first
def isSymmetric_2(self, root):
if root is None:
return True
queue = collections.deque()
queue.append(root.left)
queue.append(root.right)
while len(queue)>0:
t1 = queue.popleft()
t2 = queue.popleft()
if t1 is None and t2 is None:
continue
if t1 is None or t2 is None or t1.val != t2.val:
return False
queue.append(t1.left)
queue.append(t2.right)
queue.append(t1.right)
queue.append(t2.left)
return True