Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8d38b26

Browse files
ybian19azl397985856
authored andcommitted
feat: 226.invert-binary-tree add Python3 implementation (azl397985856#133)
1 parent 6f35ccf commit 8d38b26

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

‎problems/226.invert-binary-tree.md‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ Google: 90% of our engineers use the software you wrote (Homebrew), but you can
3939

4040
## 代码
4141

42-
```js
42+
* 语言支持:JS,Python
43+
44+
Javascript Code:
4345

46+
```js
4447
/*
4548
* @lc app=leetcode id=226 lang=javascript
4649
*
@@ -122,3 +125,28 @@ var invertTree = function(root) {
122125
return root;
123126
};
124127
```
128+
129+
Python Code:
130+
131+
```python
132+
# Definition for a binary tree node.
133+
# class TreeNode:
134+
# def __init__(self, x):
135+
# self.val = x
136+
# self.left = None
137+
# self.right = None
138+
139+
class Solution:
140+
def invertTree(self, root: TreeNode) -> TreeNode:
141+
if not root:
142+
return None
143+
stack = [root]
144+
while stack:
145+
node = stack.pop(0)
146+
node.left, node.right = node.right, node.left
147+
if node.left:
148+
stack.append(node.left)
149+
if node.right:
150+
stack.append(node.right)
151+
return root
152+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /