[LeetCode] 700. Search in a Binary Search Tree(Kotlin)

0

LeetCode

목록 보기
31/58
post-thumbnail

[LeetCode] 700. Search in a Binary Search Tree(Kotlin)

풀이

/**
 * 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
 * }
 */
class Solution {
    fun searchBST(root: TreeNode?, `val`: Int): TreeNode? {
        if(root == null) return null
        if(root.`val` == `val`) return root
        
        //binary search
        return if(root.`val` > `val`) searchBST(root.left, `val`) else searchBST(root.right, `val`)
    }
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글