forked from HuberTRoy/leetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymmetricTree.py
91 lines (72 loc) · 2.14 KB
/
SymmetricTree.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
判断左子树是否与右子树互为镜像。
思路:
遍历左子树,把结果放入队列中。
按照相反的左右遍历右子树,同时让压出队列顶的一项作对比。
不匹配或队列中没有足够的数据都可判为False.
效率 O(n)
beat 100%.
测试地址:
https://leetcode.com/problems/symmetric-tree/description/
"""
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
left = root.left
right = root.right
left_node = [left]
right_node = [right]
left_node_value = []
while left_node:
# if left_node:
_left = left_node.pop()
if _left:
left_node_value.append(_left.val)
else:
left_node_value.append(None)
if _left:
left_node.append(_left.right)
left_node.append(_left.left)
left_node_value.reverse()
while right_node:
_right = right_node.pop()
if left_node_value:
left_value = left_node_value.pop()
else:
return False
if _right:
if left_value != _right.val:
return False
else:
if left_value != None:
return False
if _right:
right_node.append(_right.left)
right_node.append(_right.right)
return True