Movatterモバイル変換


[0]ホーム

URL:


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

Commit952d226

Browse files
1
1 parentfcd1f83 commit952d226

File tree

2 files changed

+130
-33
lines changed

2 files changed

+130
-33
lines changed

‎.idea/workspace.xml

Lines changed: 84 additions & 33 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎leetcode/226. Invert Binary Tree.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
Invert a binary tree.
3+
4+
4
5+
/\
6+
2 7
7+
/ \ /\
8+
1 3 6 9
9+
to
10+
4
11+
/\
12+
7 2
13+
/ \ /\
14+
9 6 3 1
15+
'''
16+
17+
18+
# Definition for a binary tree node.
19+
classTreeNode(object):
20+
def__init__(self,x):
21+
self.val=x
22+
self.left=None
23+
self.right=None
24+
25+
classSolution(object):
26+
definvertTree(self,root):
27+
ifnotroot:
28+
return
29+
ifnotroot.leftandnotroot.right:
30+
returnroot
31+
root.left,root.right=self.invertTree(root.right),self.invertTree(root.left)
32+
returnroot
33+
34+
definvertTree2(self,root):
35+
ifroot:
36+
root.left,root.right=self.invertTree(root.right),self.invertTree(root.left)
37+
returnroot
38+
# interative
39+
definvertTreeInter(self,root):
40+
stack= [root]
41+
whilestack:
42+
node=stack.pop()
43+
ifnode:
44+
node.left,node.right=node.right,node.left
45+
stack+=node.left,node.right
46+
returnroot

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp