mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1829 from ZerenZhang2022/patch-3
Update 0226.翻转二叉树.md
This commit is contained in:
@ -359,7 +359,22 @@ class Solution:
|
||||
queue.append(node.right)
|
||||
return root
|
||||
```
|
||||
|
||||
迭代法:广度优先遍历(层序遍历),和之前的层序遍历写法一致:
|
||||
```python
|
||||
class Solution:
|
||||
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
if not root: return root
|
||||
from collections import deque
|
||||
que=deque([root])
|
||||
while que:
|
||||
size=len(que)
|
||||
for i in range(size):
|
||||
cur=que.popleft()
|
||||
cur.left, cur.right = cur.right, cur.left
|
||||
if cur.left: que.append(cur.left)
|
||||
if cur.right: que.append(cur.right)
|
||||
return root
|
||||
```
|
||||
### Go
|
||||
|
||||
递归版本的前序遍历
|
||||
|
Reference in New Issue
Block a user