File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
maximum-depth-of-binary-tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You canโt perform that action at this time.
0 commit comments