Created
December 27, 2019 03:01
-
-
Save luoheng23/7fbc9c8171e7cf4990687f2443418dee to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Definition for a binary tree node. | |
* type TreeNode struct { | |
* Val int | |
* Left *TreeNode | |
* Right *TreeNode | |
* } | |
*/ | |
func minDepth(root *TreeNode) int { | |
if root == nil { | |
return 0 | |
} | |
left, right := minDepth(root.Left), minDepth(root.Right) | |
min := left | |
if left == 0 || right != 0 && left > right { | |
min = right | |
} | |
return min + 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment