Update 0101.对称二叉树.md level order traversal

This leetcode problem can use level order traversal method, the difference between with the normal version is we should  judge for None.
There is my python answer, please feel free to contact with me if you have any problem.
This commit is contained in:
ExplosiveBattery
2022-06-09 02:33:55 +08:00
committed by GitHub
parent cded6c5c80
commit e354cd6e25

View File

@ -437,6 +437,31 @@ class Solution:
return True return True
``` ```
层次遍历
```python
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
que = [root]
while que:
this_level_length = len(que)
for i in range(this_level_length // 2):
# 要么其中一个是None但另外一个不是
if (not que[i] and que[this_level_length - 1 - i]) or (que[i] and not que[this_level_length - 1 - i]):
return False
# 要么两个都不是None
if que[i] and que[i].val != que[this_level_length - 1 - i].val:
return False
for i in range(this_level_length):
if not que[i]: continue
que.append(que[i].left)
que.append(que[i].right)
que = que[this_level_length:]
return True
```
## Go ## Go
```go ```go