From d3462824b8bd49d5f744221de9cf1878579ce214 Mon Sep 17 00:00:00 2001 From: Danil M Date: Sat, 27 Sep 2025 08:29:24 -0400 Subject: [PATCH 1/3] add 606 --- .../main/java/com/github/contest/Execute.kt | 16 ++++---- .../contest/binaryTree/BinaryTreeLeetcode.kt | 40 ++++++++++++++++++- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index 88d6716..b8ad025 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,9 +1,8 @@ package com.github.contest -import com.github.contest.binaryTree.findDuplicateSubtreesProdVariant -import com.github.contest.binaryTree.printTree 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 @@ -16,15 +15,14 @@ import java.util.TreeMap */ fun main() { - val tree = listOf(1, 2, 3, 4, null, 2, 4, null, null, 4).toTreeNode() - //tree.printTree() + val tree1 = listOf(1, 2, 3, 4).toTreeNode() + val tree2 = listOf(1).toTreeNode() + val tree3 = listOf(1, 2, 3, null, 4).toTreeNode() + //tree1.printTree() - findDuplicateSubtreesProdVariant(tree).apply { - this.forEach { - it.printTree() - } + tree2str(tree3).also { + println(it) } - } diff --git a/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt b/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt index 1b86f7b..3697fb3 100644 --- a/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt +++ b/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt @@ -402,6 +402,44 @@ fun findDuplicateSubtrees(root: TreeNode?): List { 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(")") + } + } + } } From 274ab62cd3b3c19d9fab6b4cdb54fb5796f93a74 Mon Sep 17 00:00:00 2001 From: Danil M Date: Sun, 28 Sep 2025 05:48:38 -0400 Subject: [PATCH 2/3] add 606 prod variant --- .../main/java/com/github/contest/Execute.kt | 7 ++++++ .../binaryTree/BinaryTreeProdVariant.kt | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index b8ad025..3fc8e85 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,6 +1,7 @@ package com.github.contest +import com.github.contest.array.isTriangle import com.github.contest.binaryTree.toTreeNode import com.github.contest.binaryTree.tree2str import com.github.contest.math.numberOfPowerfulInt @@ -15,6 +16,12 @@ import java.util.TreeMap */ fun main() { + //largestPerimeter(intArrayOf(3, 2, 3, 10, 2, 1, 4, 4)).also { println(it) } + + 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() diff --git a/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeProdVariant.kt b/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeProdVariant.kt index 9f6d86e..e9fff2e 100644 --- a/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeProdVariant.kt +++ b/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeProdVariant.kt @@ -118,3 +118,26 @@ fun findDuplicateSubtreesProdVariant(root: TreeNode?): List { } } + +/** + * 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(")") + } + } +} From e85d3241f61b001c1a97446ffeae89e5d347ff19 Mon Sep 17 00:00:00 2001 From: Danil M Date: Sun, 28 Sep 2025 08:05:25 -0400 Subject: [PATCH 3/3] add 669 --- .../contest/binaryTree/BinaryTreeLeetcode.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt b/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt index 3697fb3..60ad28f 100644 --- a/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt +++ b/contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt @@ -443,3 +443,18 @@ fun tree2str(root: TreeNode?): String { } } + +/** + * 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) + } +} +