mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
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:
@ -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
|
||||||
|
Reference in New Issue
Block a user