Skip to content

Commit 376840a

Browse files
committed
feat: Add solution for LeetCode problem 104
1 parent 5e2cd35 commit 376840a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// 104. Maximum Depth of Binary Tree
3+
// https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/12.
7+
//
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* public var val: Int
13+
* public var left: TreeNode?
14+
* public var right: TreeNode?
15+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
16+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
17+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
18+
* self.val = val
19+
* self.left = left
20+
* self.right = right
21+
* }
22+
* }
23+
*/
24+
final class Solution {
25+
func maxDepth(_ node: TreeNode?) -> Int {
26+
guard let node else { return 0 }
27+
28+
return max(maxDepth(node.left), maxDepth(node.right)) + 1
29+
}
30+
}

0 commit comments

Comments
 (0)