New answers tagged


Here's the realization with Z-function. It can be solved with prefix function as well.


serialize_tree is fine. deserialize_tree is \$O(n^2)\$ . In the case of balanced tree, it's still \$O(n\log{n})\$. To prove that to yourself, just add a counter inside deserialize_tree, or measure the runtime, vs input size. But here's why: First, you scan the serialized result to find the split between the left and the right tree. This takes linear time \$...

Top 50 recent answers are included