File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ from typing import Optional
2
+
3
+
4
+ class TreeNode :
5
+ def __init__ (self , val = 0 , left = None , right = None ):
6
+ self .val = val
7
+ self .left = left
8
+ self .right = right
9
+
10
+
11
+ class Solution :
12
+ """
13
+ - Idea: ์ฌ๊ท๋ฅผ ์ด์ฉํ์ฌ ๊ฐ ๋
ธ๋์ ์ผ์ชฝ ์์๊ณผ ์ค๋ฅธ์ชฝ ์์์ ๋ฐ๊พผ๋ค.
14
+ - Time Complexity: O(n). n์ ์ ์ฒด ๋
ธ๋์ ์๋ค.
15
+ ๋ชจ๋ ๋
ธ๋์ ๋ํด์ ๋ฐ๋ณต์ ์ผ๋ก ์ํํด์ผ ํ๊ธฐ ๋๋ฌธ์ O(n) ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค.
16
+ - Space Complexity: O(h). h๋ ํธ๋ฆฌ์ ๋์ด๋ก, ์ฌ๊ท ํธ์ถ์ผ๋ก ์ธํ ์คํ ๊ณต๊ฐ์ด ํ์ํ๋ค.
17
+ """
18
+
19
+ def invertTree (self , root : Optional [TreeNode ]) -> Optional [TreeNode ]:
20
+ if root is None :
21
+ return
22
+
23
+ root .left , root .right = root .right , root .left
24
+
25
+ self .invertTree (root .left )
26
+ self .invertTree (root .right )
27
+
28
+ return root
You canโt perform that action at this time.
0 commit comments