The original title link is here: https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/
Topic:
One-to-serialize a binary tree is-to-use pre-order 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
Exercises
Similar to serialize and deserialize Binary Tree.
Method 1 calculates whether Indegree and Outdegree add up to 0, and does not encounter a string, that is, encountering a node, indegree++. Root is not indegree, but it will be counted again outdegree, in order to compensate for root. Indegree initialized to-1.
Whenever a non-leaf node is encountered, it means that it must have two children. That is, there are two outdegree, and the corresponding Indegree is lost 2.
Finally see if Indegree is 0.
Time Complexity:o (n). Space:o (n).
Method 2 uses a stack to represent the hierarchy. In both cases, the first is the number, push into stack.
One is #, look at the top of the stack is not #, if, has been pop, each pop does not come two, until no longer is #, and finally put the current # into the stack; Push into stack if not #.
Example
_1_ / 3 2 /\ / # # # # #
Press stack 1, press stack 3, press stack 3 left leaf node #. When encountering 3 of the right leaf node, pop out two, stack top becomes 1, and then the current leaf node pressed into the stack, stack now has 1, #.
is equivalent to
_1_ / # 2 / # #
And so on, see if the last stack size is 1, and the only reservation is #.
Time Complexity:o (n). Space:o (n). String array size o (n), stack size O (logn).
AC Java:
1 Public classSolution {2 Public Booleanisvalidserialization (String preorder) {3 if(Preorder = =NULL|| Preorder.length () = = 0){4 return true;5 }6 7 //Method 1, calculate whether Indegree and Outdegree are added to 08String [] Strarr = Preorder.split (",");9 intIndegree =-1;//The root node is not indegree, but the following again Outdegree, in order to compensate for the initial Indegree 1Ten for(String str:strarr) { Oneindegree++;//did not encounter a STR, indicating that there is a node, so long there is a indegree A if(Indegree > 0){ - return false; - } the if(!str.equals ("#")) {//any non-leaf node, there are two children, Outdegree will be more than two, compared to Indegree less two. -indegree-=2; - } - } + returnIndegree = = 0; - + /* A //method 2, using stack at stack<string> Stk = new stack<string> (); - String [] Strarr = Preorder.split (","); - For (String Str:strarr) { - //If the stack top is #, indicating that it should return to the previous layer, pop two times, if the top is another #, indicating the return of another layer, and pop two times - While (Str.equals ("#") &&!stk.isempty () && Stk.peek (). Equals ("#")) { - Stk.pop (); in if (Stk.isempty ()) {//pop A #, the stack is empty, stating that there is no parent node, return false - return false; to } + Stk.pop ();//Pop out two a time - } the Stk.push (str);//whatever the current STR is, it is finally pressed into the stack * } $ return stk.size () = = 1 && stk.peek (). Equals ("#");Panax Notoginseng */ - } the}
Leetcode Verify Preorder serialization of a Binary Tree