Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSA in KOTLIN #950

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
}
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

}
}