题目描述:求二叉树的深度。 123456789101112import Foundationclass 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 ← Swift.翻转单词顺序VS左旋转字符串 Swift.数组中只出现一次的数字 → 扫描二维码,分享此文章