Skip to content

Commit a8d6085

Browse files
committed
Solution maximum - depth - of - binary - tree
1 parent 7d409d8 commit a8d6085

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// Merge_Two_Sorted_Lists.swift
3+
// Algorithm
4+
//
5+
// Created by ์•ˆ์„ธํ›ˆ on 4/22/25.
6+
//
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* public var val: Int
12+
* public var left: TreeNode?
13+
* public var right: TreeNode?
14+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
15+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
16+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
17+
* self.val = val
18+
* self.left = left
19+
* self.right = right
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
func maxDepth(_ root: TreeNode?) -> Int {
25+
guard root != nil else { return 0 } //nil์ผ ๊ฒฝ์šฐ ๊นŠ์ด 0์œผ๋กœ ์ฒ˜๋ฆฌ
26+
27+
var leftdepth = maxDepth(root?.left) //์™ผ์ชฝ ๋…ธ๋“œ์˜ ๊นŠ์ด ํƒ์ƒ‰
28+
var rightdepth = maxDepth(root?.right) //์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ์˜ ๊นŠ์ด ํƒ์ƒ‰
29+
30+
return max(leftdepth, rightdepth) + 1 //๋” ๊นŠ์€ ๊ณณ๊ณผ ์ž๊ธฐ ์ž์‹ ์„ ์œ„ํ•ด + 1์„ ํ•ด์„œ ๋ฆฌํ„ด.
31+
}
32+
}

0 commit comments

Comments
ย (0)