Created
June 15, 2015 00:26
-
-
Save codetalks-new/b585b873f405d7f80ccc to your computer and use it in GitHub Desktop.
https://leetcode.com/problems/maximum-depth-of-binary-tree/ Recursive solution swift version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func maxDepth_r(root:TreeNode?) -> Int{ | |
if root == nil{ | |
return 0 | |
} | |
if root?.left == nil && root?.right == nil{ | |
return 1 | |
} | |
let rightDepth = maxDepth_r(root?.left) | |
let leftDepth = maxDepth_r(root?.right) | |
let childDepth = max(leftDepth,rightDepth) | |
return childDepth + 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
对于这个题目而言,使用递归的解法一般的效率也还过得去.至少不会超时.