You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fun createTestData(str: String): TreeNode? {
var data = str
if (data == "[]") return null
data = data.substring(1, data.length - 1)
val split = data.split(",")
val len = split.size
val qLow = LinkedList<Int?>()
for (i in split.indices) {
split[i].let {
qLow.offer(
if (it == "null") {
null
} else {
it.toInt()
}
)
}
}
val qHigh = LinkedList<TreeNode?>()
if (qLow.isEmpty()) {
return null
}
val rv = qLow.poll() ?: return null
val root = TreeNode(rv)
qHigh.offer(root)
while (qHigh.isNotEmpty()) {
val r = qHigh.poll() ?: continue
val left = (qLow.poll()?.let { TreeNode(it) }).also { qHigh.offer(it) }
val right = (qLow.poll()?.let { TreeNode(it) }).also { qHigh.offer(it) }
r.left = left
r.right = right
}
return root
}
例如Leetcode94题,输入: [1,null,2,3],调用TreeNode.print( )函数,3会丢失
The text was updated successfully, but these errors were encountered: