Skip to content

Instantly share code, notes, and snippets.

@luoheng23
Created December 27, 2019 03:01
Show Gist options
  • Save luoheng23/7fbc9c8171e7cf4990687f2443418dee to your computer and use it in GitHub Desktop.
Save luoheng23/7fbc9c8171e7cf4990687f2443418dee to your computer and use it in GitHub Desktop.
/**
* 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