Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8e51cf5

Browse files
committed
🐱(recursion): 226. 翻转二叉树
1 parent 59359e3 commit 8e51cf5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/algorithm/recursion/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
## 226. 翻转二叉树
2+
3+
[原题链接](https://leetcode-cn.com/problems/invert-binary-tree/)
4+
5+
### 思路
6+
7+
递归。
8+
9+
```python
10+
# Definition for a binary tree node.
11+
# class TreeNode:
12+
# def __init__(self, x):
13+
# self.val = x
14+
# self.left = None
15+
# self.right = None
16+
17+
class Solution:
18+
def invertTree(self, root: TreeNode) -> TreeNode:
19+
if root is None:
20+
return root
21+
22+
# 翻转左子树
23+
left = self.invertTree(root.left)
24+
# 翻转右子树
25+
right = self.invertTree(root.right)
26+
27+
root.left, root.right = right, left
28+
29+
return root
30+
```
31+
132
## 341. 扁平化嵌套列表迭代器
233

334
[原题链接](https://leetcode-cn.com/problems/flatten-nested-list-iterator/)

0 commit comments

Comments
 (0)