Skip to content

Commit 4392f3c

Browse files
committed
update
1 parent 7c9f6fd commit 4392f3c

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

33. 对称二叉树.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
***给你一个二叉树的根节点 root , 检查它是否轴对称。***
2+
3+
![algo15](./images/algo15.jpg)
4+
5+
```
6+
class Solution(object):
7+
def isSymmetric(self, root):
8+
"""
9+
:type root: TreeNode
10+
:rtype: bool
11+
"""
12+
#如果为空或只有root节点
13+
if not root or not (root.left or root.right):
14+
return True
15+
# 用队列保存节点
16+
queue = [root.left,root.right]
17+
while queue:
18+
# 从队列中取出两个节点,再比较这两个节点
19+
left = queue.pop(0)
20+
right = queue.pop(0)
21+
# 如果两个节点都为空就继续循环,两者有一个为空就返回false
22+
if not (left or right):
23+
continue
24+
if not (left and right):
25+
return False
26+
if left.val!=right.val:
27+
return False
28+
# 将左节点的左孩子, 右节点的右孩子放入队列
29+
queue.append(left.left)
30+
queue.append(right.right)
31+
# 将左节点的右孩子,右节点的左孩子放入队列
32+
queue.append(left.right)
33+
queue.append(right.left)
34+
return True
35+
```

images/algo15.jpg

11.4 KB
Loading

0 commit comments

Comments
 (0)