Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.contest


import com.github.contest.binaryTree.findDuplicateSubtreesProdVariant
import com.github.contest.binaryTree.printTree
import com.github.contest.array.isTriangle
import com.github.contest.binaryTree.toTreeNode
import com.github.contest.binaryTree.tree2str
import com.github.contest.math.numberOfPowerfulInt
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
Expand All @@ -16,15 +16,20 @@ import java.util.TreeMap
*/

fun main() {
val tree = listOf(1, 2, 3, 4, null, 2, 4, null, null, 4).toTreeNode()
//tree.printTree()
//largestPerimeter(intArrayOf(3, 2, 3, 10, 2, 1, 4, 4)).also { println(it) }

findDuplicateSubtreesProdVariant(tree).apply {
this.forEach {
it.printTree()
}
}
isTriangle(3, 4, 4).also { println(it) }
}

fun treeLaunch() {
val tree1 = listOf(1, 2, 3, 4).toTreeNode()
val tree2 = listOf(1).toTreeNode()
val tree3 = listOf(1, 2, 3, null, 4).toTreeNode()
//tree1.printTree()

tree2str(tree3).also {
println(it)
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,59 @@ fun findDuplicateSubtrees(root: TreeNode?): List<TreeNode?> {

private fun serializeDuplicateSubTree(root: TreeNode?): String = when {
root == null -> "null"
else -> "#${root.`val`} , ${serialize(root?.left)} , ${serialize(root?.right)}"
else -> "#${root.`val`} , ${serializeDuplicateSubTree(root?.left)} , ${
serializeDuplicateSubTree(
root?.right
)
}"
}

/**
* 606. Construct String from Binary Tree
*/

fun tree2str(root: TreeNode?): String {
root ?: return "()"

val leftCall = tree2str(root.left)
val rightCall = tree2str(root.right)
val pattern = "()"

return buildString {
when {
leftCall == pattern && rightCall == pattern -> append(root.`val`)
rightCall == pattern -> {
append(root.`val`)
append("(")
append(leftCall)
append(")")
}

else -> {
append(root.`val`)
append("(")
append(if (leftCall == pattern) "" else leftCall)
append(")")
append("(")
append(rightCall)
append(")")
}
}
}
}


/**
* 669. Trim a Binary Search Tree
*/

fun trimBST(root: TreeNode?, low: Int, high: Int): TreeNode? = when {
root == null -> null
root.`val` < low -> trimBST(root.right, low, high)
root.`val` > high -> trimBST(root.left, low, high)
else -> root.also {
it.left = trimBST(it.left, low, high)
it.right = trimBST(it.right, low, high)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,26 @@ fun findDuplicateSubtreesProdVariant(root: TreeNode?): List<TreeNode?> {
}

}

/**
* 606. Construct String from Binary Tree
* Prod Variant
*/

fun tree2strProdVariant(root: TreeNode?): String = when {
root == null -> "()"
else -> buildString {
append(root.`val`)
root.left?.let {
append("(")
append(tree2strProdVariant(it))
append(")")
}
root.right?.let {
if (root.left == null) append("()")
append("(")
append(tree2strProdVariant(it))
append(")")
}
}
}