Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions DSA Kotlin/SquareRoot.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
fun mySqrt(x: Int): Int {
var s : Long = 0
var e : Long = x.toLong()

var mid : Long = s + (e-s)/2
var res : Long = 0

while(s<=e){

if(mid*mid == x.toLong())
return mid.toInt()
else if(mid*mid > x.toLong())
e = mid - 1
else{
res = mid;
s = mid + 1
}

mid = s + (e-s)/2
}

return res.toInt()
}
}
29 changes: 29 additions & 0 deletions DSA Kotlin/Tree Traversals/inOrderTraversal.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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 Inorder(root: TreeNode?,list: MutableList<Int>){

if(root==null)
return;

Inorder(root.left,list)
list.add(root.`val`)
Inorder(root.right,list)

return;
}
fun inorderTraversal(root: TreeNode?): List<Int> {

val ans = mutableListOf<Int>()
Inorder(root,ans)
return ans
}
}
30 changes: 30 additions & 0 deletions DSA Kotlin/Tree Traversals/postOrderTraversal.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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 Postorder(root: TreeNode?,ans: MutableList<Int>){

if(root==null)
return

Postorder(root.left,ans)
Postorder(root.right,ans)
ans.add(root.`val`)

return
}
fun postorderTraversal(root: TreeNode?): List<Int> {

val ans = mutableListOf<Int>()
Postorder(root,ans)
return ans

}
}
30 changes: 30 additions & 0 deletions DSA Kotlin/Tree Traversals/preOrderTraversal.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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 Preorder(root: TreeNode?,list: MutableList<Int>){

if(root==null)
return

list.add(root.`val`)
Preorder(root.left,list)
Preorder(root.right,list)

return
}
fun preorderTraversal(root: TreeNode?): List<Int> {

val ans = mutableListOf<Int>()
Preorder(root,ans)
return ans

}
}
16 changes: 16 additions & 0 deletions DSA Kotlin/palindrome_number.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
fun isPalindrome(x: Int): Boolean {
var num = x.toString()
var check = true

for(i in 0..((num.length-1)/2)){
if(num[i]!=num[num.length-1-i])
{
check = false
break;
}
}

return check
}
}