forked from lexrus/LeetCode.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path104.swift
46 lines (34 loc) · 1.04 KB
/
104.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// MaximumDepthOfBinaryTreeTest.swift
// LeetCode
//
// Created by Lex on 9/1/15.
// Copyright © 2015 Lex Tang. All rights reserved.
//
/*
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
*/
import Foundation
import XCTest
extension TreeNode {
private func _maxDepth(node: TreeNode?, _ depth: Int) -> Int {
if let node = node {
return 1 + max(_maxDepth(node.leftNode, depth), _maxDepth(node.rightNode, depth))
}
return 0
}
var maxDepth: Int {
return _maxDepth(self, 0)
}
}
class MaximumDepthOfBinaryTreeTest: XCTestCase {
func testExample() {
let t0 = TreeNode(0, TreeNode(1), TreeNode(2, TreeNode(3), TreeNode(4)))
XCTAssert(t0.maxDepth == 3, "")
let t1 = TreeNode(0)
XCTAssert(t1.maxDepth == 1, "")
let t2 = TreeNode(0, TreeNode(1, TreeNode(2, TreeNode(3, TreeNode(4, TreeNode(5))))))
XCTAssert(t2.maxDepth == 6, "")
}
}