Skip to content

Commit 51f8b4b

Browse files
authored
Create 0104.Maximum Depth of Binary Tree.swift
1 parent 62595ef commit 51f8b4b

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+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
8+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
9+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
10+
* self.val = val
11+
* self.left = left
12+
* self.right = right
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
func maxDepth(_ root: TreeNode?) -> Int {
18+
return depth(root)
19+
}
20+
21+
func depth(_ node: TreeNode?) -> Int {
22+
guard node != nil else {
23+
return 0
24+
}
25+
26+
let left = depth(node!.left)
27+
let right = depth(node!.right)
28+
return max(left,right) + 1
29+
}
30+
}

0 commit comments

Comments
 (0)