There is a leetcode problem to serialize and de-serialize BST. The challenge here is to de-serialise the string and construct the BST.
Can we serialise the BST using in-order traversal. Please post your comments.
To my understanding, BST can be either using pre-order traversal or post-order traversal. When using a pre-order traversal, the root value will be at the beginning of the string and when using post-order traversal, root value will be at the end of the string.
I have written a code-snippet using the pre-order traversal. At serialise time, I am using first byte to save the length of the node value and then adding the node value to the string. So we dont need any other encode here.
My serialization solution is simple:
Deserialization case, we should use the condition of BST that is left child value should be lesser than the root value and right child value should not be lesser than the root value. This condition is compulsory to build the same BST when using above serialize logic. Is it build the BST without using this condition and not using any None kind of markers for null leaves.
My solution of de-serialization: