Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Trying to be better than yesterday

Vamsi Tallapudi vamsitallapudi

🎯
Trying to be better than yesterday
View GitHub Profile
View PostOrderTraversalIterative.kt
import java.util.ArrayDeque
import java.util.LinkedList
fun postOrderIterative(root: TreeNode?): List<Int> {
if(root == null) return emptyList()
val stack = ArrayDeque<TreeNode>()
val list = LinkedList<Int>()
stack.push(root)
while (stack.isNotEmpty()) {
val node = stack.pop()
View PostOrderTraversalRecursive.kt
fun postOrderRecursive(root: TreeNode?) : List<Int>{
if(root == null) return emptyList()
return postOrderRecursive(root.left) + postOrderRecursive(root.right) + listOf(root.data)
}
View InOrderIterative.kt
import java.util.ArrayDeque
fun inorderTraversalIterative(root: TreeNode?): List<Int> {
val list = mutableListOf<Int>()
if (root == null) return list
var node = root
val stack = ArrayDeque<TreeNode>()
// traversing the tree whenever right node is not null or the stack contains items
while (node != null || stack.isNotEmpty()) {
// processing all the left nodes of the current node
View InorderRecursive.kt
fun inOrderTraversalRecursive(root: TreeNode?) : List<Int> {
if (root == null) return emptyList()
return inOrderTraversalRecursive(root.left) + listOf(root.data) + inOrderTraversalRecursive(root.right)
}
View PreOrderTraversalIterative.kt
import java.util.ArrayDeque
fun preOrderTraversalIterative(root: TreeNode?): List<Int> {
val myList = mutableListOf<Int>()
// creating stack to store the left and right nodes while processing root node
val stack = ArrayDeque<TreeNode>()
// checking edge case and returning empty list
if (root == null) return myList
var node = root
while (node != null || stack.isNotEmpty()) {
View TreeNode.kt
class TreeNode(var data: Int, var left: TreeNode? = null, var right: TreeNode? = null)
fun initializeBinaryTree(): TreeNode {
val c = TreeNode(4)
val d = TreeNode(5)
val a = TreeNode(2, c, d)
val b = TreeNode(3)
return TreeNode(1, a, b)
}
View PreOrderTraversal.kt
fun preorderTraversal(root: TreeNode?): List<Int> {
// if root is null, return list of empty array
if (root == null) {
return listOf()
}
// iterate recursively into left child and right child
return listOf(root.data) + preorderTraversal(root.left) + preorderTraversal(root.right)
}
View PreOrderTraversal.py
class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
# to return an empty array if root is empty
if not root:
return []
# appending the root value in array and recursively going into left and right subtrees
return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)
View TreeNode.py
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def initializeBinaryTree() -> TreeNode:
a = TreeNode(2)
b = TreeNode(3)
View PrefLocalDataSourceImpl.kt
class PrefLocalDataSourceImpl(val context: Context) : PrefRepo {
override fun updateDarkMode(enabled: Boolean) {
val sharedPreferences = context.getSharedPreferences("darkModePref", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putBoolean("darkMode", true)
editor.apply()
}
}