cubegao

Swift.二叉树的深度

2016-05-01

题目描述:求二叉树的深度。

1
2
3
4
5
6
7
8
9
10
11
import Foundation

class For39Solution {
func treeDepth(_ root: TreeNode?) -> Int {
if root == nil {
return 0
}

return max(treeDepth(root?.left), treeDepth(root?.right)) + 1
}
}

算法思想:递归求,每次+1,利用栈的特性。

github地址:https://github.com/cubegao/LeetCode

Tags: 算法

扫描二维码,分享此文章