Created
February 6, 2020 16:03
-
-
Save vitek999/a343af08670cf465ae8147f1cbe3d8fc 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
/** | |
* 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