diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index 88d6716..3fc8e85 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -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 @@ -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) + } } 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..60ad28f 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,59 @@ 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(")") + } + } + } +} + + +/** + * 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) + } } 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(")") + } + } +}