Skip to content

Instantly share code, notes, and snippets.

@vitek999
Created February 6, 2020 16:03
Show Gist options
  • Save vitek999/a343af08670cf465ae8147f1cbe3d8fc to your computer and use it in GitHub Desktop.
Save vitek999/a343af08670cf465ae8147f1cbe3d8fc to your computer and use it in GitHub Desktop.
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
import kotlin.math.min
import kotlin.math.max
class Solution {
fun minDepth(root: TreeNode?): Int {
if (root == null) return 0
val leftCount = 1 + minDepth(root.left)
val rightCount = 1 + minDepth(root.right)
return if (root.left == null || root.right == null)
max(leftCount, rightCount)
else
min(leftCount, rightCount)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment