One-to-serialize a binary tree is-to-use pre-oder traversal. When we encounter a non-null node, we record the node ' s value. If It is a null node, we record using a Sentinel value such as # .
_9_ / 3 2 /\ / 4 1 # 6/\/\ /# # # # # # # # # #
For example, the above binary tree can "9,3,4,#,#,1,#,#,2,#,6,#,#" is serialized to the string, where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree . Find an algorithm without reconstructing the tree.
Each comma separated value in the string must is either an integer or a character ‘#‘ representing null pointer.
Assume that the input format is always valid, for example it could never contain, consecutive commas such as .
Example 1:
"9,3,4,#,#,1,#,#,2,#,6,#,#"
Returntrue
Example 2:
"1,#"
Returnfalse
Example 3:
"9,#,#,1"
Returnfalse
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
This problem gives us a string similar to the serialized binary tree, and the serialization and de-serialization of the binary tree can be found in my previous blog serialize and deserialize Binary tree, This question lets us judge whether the given string is a string that is not a correctly serialized two-tree. Then according to the previous blog's solution, we still have to use Istringsteam to manipulate the string, C + + there is no string like Java Split function, you can directly separate arbitrary strings, we can only use getline this function, To store the contents of the string stream in a vector array. We can observe the following two rules by lifting some of the correct examples, such as "9,3,4,#,#,1,#,#,2,#,6,#,#" or "9,3,4,#,#,1,#,#,2,#,6,#,#", and so on:
1. Number is always less than # number one
2. The last one must be the # number
Then we join first not to consider the last #, then the number and # number should be the same, if we initialize a counter to 0, hit the number, counter plus 1, encountered #, the counter minus 1, then the last counter should be 0. Let's look at two examples of return false, "#,7,6,9,#,#,#" and "7,2,#,2,#,#,#,6,#", so we can see from the two counter examples that if the root node is empty, there can be no more nodes, and no more than three consecutive # numbers can appear. So we add the counter, if we encounter the # number, and at this time the counter is 0, and then minus the negative, and then directly return false, because in the correct sequence, any one position I, in the [0, I] in the range of # numbers is not more than the number of digits. When the loop is complete, we detect if the counter is 0 and see if the last character is the # number. See the code below:
classSolution { Public: BOOLIsvalidserialization (stringpreorder) { if(Preorder.empty ())return false; Istringstreaminch(preorder); Vector<string>v; stringVal; intD =0; while(Getline (inch, Val,',') ) V.push_back (Val); for(inti =0; I < v.size ()-1; ++i) {if(V[i] = ="#") { if(d = =0)return false; Else--D; } Else++D; } returnD! =0?false: v.back () = ="#"; }};
Similar topics:
Serialize and Deserialize Binary Tree
Resources:
Https://leetcode.com/discuss/83809/simple-o-n-solution
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Verify preorder serialization of a binary tree verifying the sequencing sequence of binary trees