File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
contest/src/main/java/com/github/contest/binaryTree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -28,4 +28,26 @@ class FindElementsAlternativeSolution(root: TreeNode?) {
2828 fun find (target : Int ): Boolean {
2929 return values.contains(target)
3030 }
31+ }
32+
33+ /* *
34+ * 572. Subtree of Another Tree
35+ * Alternative Solution
36+ * Serialization
37+ */
38+
39+ fun isSubtreeSerialization (root : TreeNode ? , subRoot : TreeNode ? ): Boolean {
40+
41+ val stringRoot = serialize(root)
42+ val stringSubRoot = serialize(subRoot)
43+
44+ return stringRoot.contains(stringSubRoot)
45+ }
46+
47+
48+ fun serialize (root : TreeNode ? ): String {
49+ if (root == null ) return " null"
50+
51+ // Use preorder traversal with markers
52+ return " #${root.`val `} ${serialize(root.left)} ${serialize(root.right)} "
3153}
You can’t perform that action at this time.
0 commit comments