mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
0226.翻转二叉树.md 修正几处 Python 代码不严谨说法
This commit is contained in:
@ -81,7 +81,7 @@ if (root == NULL) return root;
|
|||||||
|
|
||||||
3. 确定单层递归的逻辑
|
3. 确定单层递归的逻辑
|
||||||
|
|
||||||
因为是先前序遍历,所以先进行交换左右孩子节点,然后反转左子树,反转右子树。
|
因为是前序遍历,所以先进行交换左右孩子节点,然后反转左子树,反转右子树。
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
swap(root->left, root->right);
|
swap(root->left, root->right);
|
||||||
@ -348,14 +348,13 @@ class Solution:
|
|||||||
while stack:
|
while stack:
|
||||||
node = stack.pop()
|
node = stack.pop()
|
||||||
node.left, node.right = node.right, node.left
|
node.left, node.right = node.right, node.left
|
||||||
if node.left:
|
|
||||||
stack.append(node.left)
|
|
||||||
if node.right:
|
if node.right:
|
||||||
stack.append(node.right)
|
stack.append(node.right)
|
||||||
|
if node.left:
|
||||||
|
stack.append(node.left)
|
||||||
return root
|
return root
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
递归法:中序遍历:
|
递归法:中序遍历:
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
@ -374,7 +373,7 @@ class Solution:
|
|||||||
return root
|
return root
|
||||||
```
|
```
|
||||||
|
|
||||||
迭代法:中序遍历:
|
迭代法,伪中序遍历(结果是对的,看起来像是中序遍历,实际上它是前序遍历,只不过把中间节点处理逻辑放到了中间。还是要用'统一写法'才是真正的中序遍历):
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode:
|
# class TreeNode:
|
||||||
@ -389,15 +388,14 @@ class Solution:
|
|||||||
stack = [root]
|
stack = [root]
|
||||||
while stack:
|
while stack:
|
||||||
node = stack.pop()
|
node = stack.pop()
|
||||||
if node.left:
|
if node.right:
|
||||||
stack.append(node.left)
|
stack.append(node.right)
|
||||||
node.left, node.right = node.right, node.left
|
node.left, node.right = node.right, node.left # 放到中间,依然是前序遍历
|
||||||
if node.left:
|
if node.right:
|
||||||
stack.append(node.left)
|
stack.append(node.right)
|
||||||
return root
|
return root
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
递归法:后序遍历:
|
递归法:后序遍历:
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
@ -416,7 +414,7 @@ class Solution:
|
|||||||
return root
|
return root
|
||||||
```
|
```
|
||||||
|
|
||||||
迭代法:后序遍历:
|
迭代法,伪后序遍历(结果是对的,看起来像是后序遍历,实际上它是前序遍历,只不过把中间节点处理逻辑放到了最后。还是要用'统一写法'才是真正的后序遍历):
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode:
|
# class TreeNode:
|
||||||
@ -431,19 +429,15 @@ class Solution:
|
|||||||
stack = [root]
|
stack = [root]
|
||||||
while stack:
|
while stack:
|
||||||
node = stack.pop()
|
node = stack.pop()
|
||||||
if node.left:
|
|
||||||
stack.append(node.left)
|
|
||||||
if node.right:
|
if node.right:
|
||||||
stack.append(node.right)
|
stack.append(node.right)
|
||||||
|
if node.left:
|
||||||
|
stack.append(node.left)
|
||||||
node.left, node.right = node.right, node.left
|
node.left, node.right = node.right, node.left
|
||||||
|
|
||||||
return root
|
return root
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
迭代法:广度优先遍历(层序遍历):
|
迭代法:广度优先遍历(层序遍历):
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
|
Reference in New Issue
Block a user