Skip to content

Instantly share code, notes, and snippets.

class Solution {
val leaves = mutableListOf<Pair<TreeNode, Int>>()
var depth = 1
fun deepestLeavesSum(root: TreeNode?): Int {
if (root == null) return 0
calcdepth(root)
val maxdepth = leaves.map { it.second }.maxBy { it }
return leaves.filter { it.second == maxdepth }.sumBy { it.first.`val` }
}
@yushman
yushman / MyAdapter.kt
Last active April 20, 2022 15:14
Adding listener to RecyclerView Items & Views
// !!!
// If you need to handle clicking on the entire element only, you can omit adding View to listener lambda - listener(myModel)
// so in "bind" method you can write only - view.setOnclickListener { listener.invoke(myViewModel) }
class MyAdapter (private val listener: (MyModel, View) -> Unit): RecyclerView.Adapter<MyAdapter.MyViewHolder>{
val myItemList = listOf<MyModel>()
//...
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(myItemList[position], listener) //bind listener to position
}