We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
There's no set up code for doing this in Kotlin, so I wrote some.
If you use this, you just need to create 'fun findLowestCommonAncestor(v1: Int, v2: Int, root: Node): Int'
dataclassNode(varleft:Node?,varright:Node?,vardata:Int)funplaceNode(head:Node,new:Node){// leftif(head.data>new.data){if(head.left==null){// println("Place Left " + new.data)head.left=new}else{placeNode(head.left!!,new)}}// rightif(head.data<new.data){if(head.right==null){//println("Place Right " + new.data)head.right=new}else{placeNode(head.right!!,new)}}}funmakeBinaryTree(ints:List<Int>):Node?{if(ints.size<1)returnnullvalroot=Node(null,null,ints[0])for(iin1..ints.size-1){valnode=Node(null,null,ints[i])placeNode(root,node)}returnroot}funmain(args:Array<String>){/* Enter your code here. Read input from STDIN. Print output to STDOUT. */valscan=Scanner(System.`in`)valnodeCount=scan.nextLine().trim().toInt()valitem=scan.nextLine()valitems=item.split(" ").map{it.toInt()}//println(items.joinToString())valvalue=scan.nextLine()valvalues=value.split(" ")//println(values.joinToString())valv1=values[0].toInt()valv2=values[1].toInt()valroot=makeBinaryTree(items)println(findLowestCommonAncestor(v1,v2,root!!))}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Binary Search Tree : Lowest Common Ancestor
You are viewing a single comment's thread. Return to all comments →
There's no set up code for doing this in Kotlin, so I wrote some. If you use this, you just need to create 'fun findLowestCommonAncestor(v1: Int, v2: Int, root: Node): Int'