forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEGON.py
48 lines (36 loc) · 1.31 KB
/
EGON.py
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
38
39
40
41
42
43
44
45
46
47
48
from typing import Optional
from unittest import TestCase, main
# 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
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
return self.solve_dfs(root)
"""
Runtime: 0 ms (Beats 100.00%)
Time Complexity: O(n)
> 트리의 모든 node를 방문하므로 O(n)
Memory: 16.53 MB (Beats 25.95%)
Space Complexity: O(n)
> stack의 최대 크기는 트리의 최장 경로를 이루는 node의 갯수이고, 최악의 경우 트리의 한 쪽으로 모든 node가 이어져있는 경우이므로 O(n), upper bound
"""
def solve_dfs(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return root
stack = [root]
while stack:
curr_node = stack.pop()
curr_node.left, curr_node.right = curr_node.right, curr_node.left
if curr_node.left:
stack.append(curr_node.left)
if curr_node.right:
stack.append(curr_node.right)
return root
class _LeetCodeTestCases(TestCase):
def test_1(self):
return
if __name__ == '__main__':
main()