forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhi-rachel.py
More file actions
37 lines (32 loc) ยท 906 Bytes
/
hi-rachel.py
File metadata and controls
37 lines (32 loc) ยท 906 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
30
31
32
33
34
35
36
37
from typing import Optional
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
"""
์ฌ๊ท ํ์ด
TC: O(n), SC: O(n)
n = ํธ๋ฆฌ ๋ด์ ๋
ธ๋ ์
"""
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
return root
"""
์คํ ํ์ด
TC: O(n), SC: O(n)
"""
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
stack = [root]
while stack:
node = stack.pop()
if not node:
continue
node.left, node.right = node.right, node.left
stack += [node.left, node.right]
return root