Skip to content

Commit 0e41479

Browse files
Merge pull request #1 from ExplosiveBattery/ExplosiveBattery-patch-0101
Update 0101.对称二叉树.md level order traversal
2 parents cded6c5 + e354cd6 commit 0e41479

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

problems/0101.对称二叉树.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,31 @@ class Solution:
437437
return True
438438
```
439439

440+
层次遍历
441+
```python
442+
class Solution:
443+
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
444+
if not root:
445+
return True
446+
447+
que = [root]
448+
while que:
449+
this_level_length = len(que)
450+
for i in range(this_level_length // 2):
451+
# 要么其中一个是None但另外一个不是
452+
if (not que[i] and que[this_level_length - 1 - i]) or (que[i] and not que[this_level_length - 1 - i]):
453+
return False
454+
# 要么两个都不是None
455+
if que[i] and que[i].val != que[this_level_length - 1 - i].val:
456+
return False
457+
for i in range(this_level_length):
458+
if not que[i]: continue
459+
que.append(que[i].left)
460+
que.append(que[i].right)
461+
que = que[this_level_length:]
462+
return True
463+
```
464+
440465
## Go
441466

442467
```go

0 commit comments

Comments
 (0)