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 9ccd081 commit 959c10cCopy full SHA for 959c10c
โmaximum-depth-of-binary-tree/thispath98.pyโ
@@ -0,0 +1,25 @@
1
+class Solution:
2
+ def maxDepth(self, root: Optional[TreeNode]) -> int:
3
+ """
4
+ Intuition:
5
+ ์ผ์ชฝ ์์๊ณผ ์ค๋ฅธ์ชฝ ์์์ ๋ํด depth๋ฅผ ์ฆ๊ฐ์ํค๋ฉฐ ์ฌ๊ทํ๊ณ ,
6
+ ๋ ์ค ํฐ ๊ฐ์ ๋ฐํํ๋ค.
7
+
8
+ Time Complexity:
9
+ O(N):
10
+ ๋ชจ๋ ๋ ธ๋์ ๋ํด ์ฌ๊ท์ ์ผ๋ก ํธ์ถํ๋ฏ๋ก O(N)์ด๋ค.
11
12
+ Space Complexity:
13
+ O(1):
14
+ answer ๋ณ์๋ง ์ฌ์ฉํ๋ฏ๋ก O(1)์ด๋ค.
15
16
+ def get_depth(node, depth):
17
+ if not node:
18
+ return depth
19
20
+ left = get_depth(node.left, depth + 1)
21
+ right = get_depth(node.right, depth + 1)
22
+ return max(left, right)
23
24
+ answer = get_depth(root, 0)
25
+ return answer
0 commit comments