mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Add level order traversal
This commit is contained in:
@ -437,6 +437,41 @@ class Solution:
|
|||||||
return True
|
return True
|
||||||
```
|
```
|
||||||
|
|
||||||
|
层序遍历
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def isSymmetric(self, root: TreeNode) -> bool:
|
||||||
|
if not root: return True
|
||||||
|
que, cnt = [[root.left, root.right]], 1
|
||||||
|
while que:
|
||||||
|
nodes, tmp, sign = que.pop(), [], False
|
||||||
|
for node in nodes:
|
||||||
|
if not node:
|
||||||
|
tmp.append(None)
|
||||||
|
tmp.append(None)
|
||||||
|
else:
|
||||||
|
if node.left:
|
||||||
|
tmp.append(node.left)
|
||||||
|
sign = True
|
||||||
|
else:
|
||||||
|
tmp.append(None)
|
||||||
|
if node.right:
|
||||||
|
tmp.append(node.right)
|
||||||
|
sign = True
|
||||||
|
else:
|
||||||
|
tmp.append(None)
|
||||||
|
p1, p2 = 0, len(nodes) - 1
|
||||||
|
while p1 < p2:
|
||||||
|
if (not nodes[p1] and nodes[p2]) or (nodes[p1] and not nodes[p2]): return False
|
||||||
|
elif nodes[p1] and nodes[p2] and nodes[p1].val != nodes[p2].val: return False
|
||||||
|
p1 += 1
|
||||||
|
p2 -= 1
|
||||||
|
if sign: que.append(tmp)
|
||||||
|
cnt += 1
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user