Skip to content

Commit 5a411b0

Browse files
committed
Add: Implement iterative solution
1 parent 5f8b4bd commit 5a411b0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

maximum-depth-of-binary-tree/KwonNayeon.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
# self.val = val
2323
# self.left = left
2424
# self.right = right
25+
26+
# Solution 1: 재귀
2527
class Solution:
2628
def maxDepth(self, root: Optional[TreeNode]) -> int:
2729
if root is None:
@@ -31,3 +33,24 @@ def maxDepth(self, root: Optional[TreeNode]) -> int:
3133
right_depth = self.maxDepth(root.right)
3234

3335
return max(left_depth, right_depth) + 1
36+
37+
# Solution 2: 반복문
38+
class Solution:
39+
def maxDepth(self, root: Optional[TreeNode]) -> int:
40+
if root is None:
41+
return 0
42+
43+
stack = [(root, 1)]
44+
max_depth = 0
45+
46+
while stack:
47+
current, depth = stack.pop()
48+
49+
max_depth = max(max_depth, depth)
50+
51+
if current.left:
52+
stack.append((current.left, depth + 1))
53+
if current.right:
54+
stack.append((current.right, depth + 1))
55+
56+
return max_depth

merge-intervals/KwonNayeon.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Constraints:
3+
- 1 <= intervals.length <= 10^4
4+
- intervals[i].length == 2
5+
- 0 <= starti <= endi <= 10^4
6+
7+
Time Complexity:
8+
-
9+
10+
Space Complexity:
11+
-
12+
13+
풀이방법:
14+
1.
15+
"""

0 commit comments

Comments
 (0)