forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmangodm-web.py
More file actions
29 lines (21 loc) ยท 918 Bytes
/
mangodm-web.py
File metadata and controls
29 lines (21 loc) ยท 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from typing import Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
"""
- Idea: ์ฌ๊ท๋ฅผ ์ด์ฉํ์ฌ ๊ฐ ๋
ธ๋์ ์ผ์ชฝ ์์๊ณผ ์ค๋ฅธ์ชฝ ์์์ ๋ฐ๊พผ๋ค.
- Time Complexity: O(n). n์ ์ ์ฒด ๋
ธ๋์ ์๋ค.
๋ชจ๋ ๋
ธ๋์ ๋ํด์ ๋ฐ๋ณต์ ์ผ๋ก ์ํํด์ผ ํ๊ธฐ ๋๋ฌธ์ O(n) ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค.
- Space Complexity: O(n). n์ ์ ์ฒด ๋
ธ๋์ ์๋ค.
์ต์
์ ๊ฒฝ์ฐ, ๋ถ๊ท ํ ํธ๋ฆฌ์์๋ ์ฌ๊ท ํธ์ถ๋ก ์ธํด ์ต๋ O(n) ์คํ ๊ณต๊ฐ์ด ํ์ํ๋ค.
"""
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return
root.left, root.right = root.right, root.left
self.invertTree(root.left)
self.invertTree(root.right)
return root