Skip to content

Commit 922e21d

Browse files
Merge pull request #222
add new problem 28.09
2 parents 125a515 + e85d324 commit 922e21d

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.contest
22

33

4-
import com.github.contest.binaryTree.findDuplicateSubtreesProdVariant
5-
import com.github.contest.binaryTree.printTree
4+
import com.github.contest.array.isTriangle
65
import com.github.contest.binaryTree.toTreeNode
6+
import com.github.contest.binaryTree.tree2str
77
import com.github.contest.math.numberOfPowerfulInt
88
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
99
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
@@ -16,15 +16,20 @@ import java.util.TreeMap
1616
*/
1717

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

22-
findDuplicateSubtreesProdVariant(tree).apply {
23-
this.forEach {
24-
it.printTree()
25-
}
26-
}
21+
isTriangle(3, 4, 4).also { println(it) }
22+
}
2723

24+
fun treeLaunch() {
25+
val tree1 = listOf(1, 2, 3, 4).toTreeNode()
26+
val tree2 = listOf(1).toTreeNode()
27+
val tree3 = listOf(1, 2, 3, null, 4).toTreeNode()
28+
//tree1.printTree()
29+
30+
tree2str(tree3).also {
31+
println(it)
32+
}
2833
}
2934

3035

contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,59 @@ fun findDuplicateSubtrees(root: TreeNode?): List<TreeNode?> {
402402

403403
private fun serializeDuplicateSubTree(root: TreeNode?): String = when {
404404
root == null -> "null"
405-
else -> "#${root.`val`} , ${serialize(root?.left)} , ${serialize(root?.right)}"
405+
else -> "#${root.`val`} , ${serializeDuplicateSubTree(root?.left)} , ${
406+
serializeDuplicateSubTree(
407+
root?.right
408+
)
409+
}"
410+
}
411+
412+
/**
413+
* 606. Construct String from Binary Tree
414+
*/
415+
416+
fun tree2str(root: TreeNode?): String {
417+
root ?: return "()"
418+
419+
val leftCall = tree2str(root.left)
420+
val rightCall = tree2str(root.right)
421+
val pattern = "()"
422+
423+
return buildString {
424+
when {
425+
leftCall == pattern && rightCall == pattern -> append(root.`val`)
426+
rightCall == pattern -> {
427+
append(root.`val`)
428+
append("(")
429+
append(leftCall)
430+
append(")")
431+
}
432+
433+
else -> {
434+
append(root.`val`)
435+
append("(")
436+
append(if (leftCall == pattern) "" else leftCall)
437+
append(")")
438+
append("(")
439+
append(rightCall)
440+
append(")")
441+
}
442+
}
443+
}
444+
}
445+
446+
447+
/**
448+
* 669. Trim a Binary Search Tree
449+
*/
450+
451+
fun trimBST(root: TreeNode?, low: Int, high: Int): TreeNode? = when {
452+
root == null -> null
453+
root.`val` < low -> trimBST(root.right, low, high)
454+
root.`val` > high -> trimBST(root.left, low, high)
455+
else -> root.also {
456+
it.left = trimBST(it.left, low, high)
457+
it.right = trimBST(it.right, low, high)
458+
}
406459
}
407460

contest/src/main/java/com/github/contest/binaryTree/BinaryTreeProdVariant.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,26 @@ fun findDuplicateSubtreesProdVariant(root: TreeNode?): List<TreeNode?> {
118118
}
119119

120120
}
121+
122+
/**
123+
* 606. Construct String from Binary Tree
124+
* Prod Variant
125+
*/
126+
127+
fun tree2strProdVariant(root: TreeNode?): String = when {
128+
root == null -> "()"
129+
else -> buildString {
130+
append(root.`val`)
131+
root.left?.let {
132+
append("(")
133+
append(tree2strProdVariant(it))
134+
append(")")
135+
}
136+
root.right?.let {
137+
if (root.left == null) append("()")
138+
append("(")
139+
append(tree2strProdVariant(it))
140+
append(")")
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)