We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2dccaf5 commit 4615d6bCopy full SHA for 4615d6b
1 file changed
โmaximum-depth-of-binary-tree/ohkingtaek.pyโ
@@ -0,0 +1,25 @@
1
+# Definition for a binary tree node.
2
+# class TreeNode:
3
+# def __init__(self, val=0, left=None, right=None):
4
+# self.val = val
5
+# self.left = left
6
+# self.right = right
7
+class Solution:
8
+ def maxDepth(self, root: Optional[TreeNode]) -> int:
9
+ """
10
+ ์๊ฐ๋ณต์ก๋: O(n)
11
+ ๊ณต๊ฐ๋ณต์ก๋: O(n)
12
+ ์ด์ง ํธ๋ฆฌ ํ์ํ๋ฉฐ ๋์ค ์ฆ๊ฐ ๊ณ์ ํ๋ฉฐ ์งํ
13
14
+ depth = 0
15
+ if not root:
16
+ return 0
17
+ array = [(root, 1)]
18
+ while array:
19
+ node, d = array.pop()
20
+ depth = max(d, depth)
21
+ if node.left:
22
+ array.append((node.left, d + 1))
23
+ if node.right:
24
+ array.append((node.right, d + 1))
25
+ return depth
0 commit comments