thhh
04/06/2021, 1:10 PMfalse
boolean here at once.
Details in threadthhh
04/06/2021, 1:16 PMfind()
inside the if statement root == null
See if root == null
is true, then the find
function must return false
right?
But what happens according to debugging is:
Only should print once BUT it print multiple times (!?) --> null
Only should print once BUT it print multiple times (!?) --> null
Only should print once and it prints one time only as expected!
result true
The important part in the above println logs is Only should print once BUT it print multiple times (!?) --> null
is root == null
is true and we are entering the if
block, so the function find()
at once MUST return true
and exit right?
But what happens it, the code is seemingly ignoring that and not returning false
and instead it's keep on executing!! and here is where I am confused!
Code below:
class TwoSumIV {
`class TreeNode(var `val`: Int = 0) {`
var left: TreeNode? = null
var right: TreeNode? = null
}
fun findTarget(root: TreeNode?, k: Int): Boolean {
val set: MutableSet<Int?> = HashSet<Int?>()
return find(root, k, set)
}
fun find(root: TreeNode?, k: Int, set: MutableSet<Int?>): Boolean {
if (root == null) {
`println("Only should print once BUT it print multiple times (!?) --> ${root?.val
}")`
return false
`} else if (set.contains(k - root.val
)) {`
println("Only should print once and it prints one time only as expected!")
return true
}
`set.add(root.val
)`
return find(root.left, k, set) || find(root.right, k, set)
}
}
fun main() {
val root = TwoSumIV.TreeNode(5)
root.left = TwoSumIV.TreeNode(3)
root.right = TwoSumIV.TreeNode(6)
println(root)
val twoSumIV = TwoSumIV()
val result = twoSumIV.findTarget(root, 9)
println("result $result")
}
Hadji Musaev
04/06/2021, 1:29 PMk - val
is in the set. 9 - 6 = 3
is indeed present, so it returns true
.tddmonkey
04/06/2021, 1:31 PMHadji Musaev
04/06/2021, 1:31 PMprintln
that you expect to only be executed once, will in fact be executed as many times as many leaves you visit before terminatingMichael Böiers
04/06/2021, 8:48 PMtherealbluepandabear
04/07/2021, 2:31 AMMichael Böiers
04/07/2021, 6:55 AMdata class Node(val value: Int = 0, val left: Node? = null, val right: Node? = null)
class TwoSumIV {
fun findTarget(n: Node, k: Int) = find(n, k, HashSet())
private fun find(n: Node?, k: Int, set: MutableSet<Int>): Boolean = when {
n == null -> false
set.contains(k - n.value) -> true
else -> {
set.add(n.value)
with (n) {
sequenceOf(left, right).any { find(it, k, set) }
}
}
}
}
fun main() {
Node(5, left = Node(3), right = Node(6)).let {
println("result ${TwoSumIV().findTarget(it, k = 9)}")
}
}
tddmonkey
04/07/2021, 6:59 AMtddmonkey
04/07/2021, 6:59 AMfind
method to be much harder to read than the originalMichael Böiers
04/07/2021, 6:59 AMwith
, which was a little over the top.tddmonkey
04/07/2021, 7:10 AMMichael Böiers
04/07/2021, 7:20 AMMichael Böiers
04/07/2021, 7:22 AMwith
just because I canât use a block of code in the boolean expression directly.
data class Node(val value: Int = 0, val left: Node? = null, val right: Node? = null) {
val children = sequenceOf(left, right)
}
class TwoSumIV {
fun findTarget(n: Node, k: Int) = n.find(k, HashSet())
private fun Node.find(k: Int, set: MutableSet<Int>): Boolean =
set.contains(k - value) || with (children) {
set.add(value)
filterNotNull().any { it.find(k, set) }
}
}
fun main() {
Node(5, left = Node(3), right = Node(6)).let {
println(it)
println("result ${TwoSumIV().findTarget(it, k = 9)}")
}
}
Michael Böiers
04/07/2021, 7:29 AMdata class Node(val value: Int = 0, val left: Node? = null, val right: Node? = null) {
val children = listOfNotNull(left, right)
}
class TwoSumIV {
fun findTarget(n: Node, k: Int) = n.find(k, HashSet())
private fun Node.find(k: Int, set: MutableSet<Int>): Boolean =
k - value in set || with (children) {
set += value
any { it.find(k, set) }
}
}
fun main() {
Node(5, left = Node(3), right = Node(6)).let {
println(it)
println("result ${TwoSumIV().findTarget(it, k = 9)}")
}
}
tddmonkey
04/07/2021, 8:32 AMsealed class TreeNode(val value: Int) {
abstract fun find(k: Int): Boolean
}
class LeafNode(value: Int): TreeNode(value) {
override fun find(k: Int): Boolean {
return value == k
}
}
class BranchNode(value: Int, val left: TreeNode, val right: TreeNode): TreeNode(value) {
override fun find(k: Int): Boolean {
if (value == k) {
return true
}
return left.find(k) || right.find(k)
}
}
class TwoSumIV {
fun findTarget(root: TreeNode, k: Int): Boolean {
return root.find(k)
}
}
fun main() {
val root = BranchNode(5, left = LeafNode(3), right = LeafNode(6))
println(root)
val twoSumIV = TwoSumIV()
println("Tree has 6 -> ${twoSumIV.findTarget(root, 6)}")
println("Tree has 9 -> ${twoSumIV.findTarget(root, 9)}")
}
Michael Böiers
04/07/2021, 8:42 AMdata class Node(val value: Int, val left: Node? = null, val right: Node? = null) {
private val children = listOfNotNull(left, right)
fun find(k: Int): Boolean = value == k || children.any { it.find(k) }
}
fun leafOf(value: Int) = Node(value)
fun branchOf(value: Int, left: Node, right: Node) = Node(value, left, right)
class TwoSumIV {
fun findTarget(root: Node, k: Int) = root.find(k)
}
fun main() {
val root = branchOf(5, left = leafOf(3), right = leafOf(6))
val twoSumIV = TwoSumIV()
println("Tree has 6 -> ${twoSumIV.findTarget(root, 6)}")
println("Tree has 9 -> ${twoSumIV.findTarget(root, 9)}")
}
Michael Böiers
04/07/2021, 8:43 AMtddmonkey
04/07/2021, 8:52 AMMichael Böiers
04/07/2021, 8:56 AMMichael Böiers
04/07/2021, 8:59 AMtddmonkey
04/07/2021, 9:15 AMLeafNode
.Michael Böiers
04/07/2021, 10:07 AMsealed class Node {
data class Leaf(val value: Int) : Node()
data class Branch(val value: Int, val left: Node, val right: Node) : Node()
fun find(k: Int): Boolean = when (this) {
is Leaf -> k == value
is Branch -> k == value || left.find(k) || right.find(k)
}
}
class TwoSumIV {
fun findTarget(root: Node, k: Int): Boolean {
return root.find(k)
}
}
fun main() {
val root = Node.Branch(5, left = Node.Leaf(3), right = Node.Leaf(6))
println(root)
val twoSumIV = TwoSumIV()
println("Tree has 6 -> ${twoSumIV.findTarget(root, 6)}")
println("Tree has 9 -> ${twoSumIV.findTarget(root, 9)}")
}
tddmonkey
04/07/2021, 10:36 AMMichael Böiers
04/07/2021, 10:58 AMHadji Musaev
04/07/2021, 5:32 PM/**
* 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
* }
*/