Verify preorder serialization of a Binary Tree

Source: Internet
Author: User

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

Analysis: https://www.hrwhisper.me/leetcode-verify-preorder-serialization-of-a-binary-tree/

The simple way to do this is to constantly cut down the leaf nodes. Finally see if I can cut it all off.

As an example, "9,3,4,#,#,1,#,#,2,#,6,#,#" encounters an X # #的时候 and turns it into a #

I simulate the process again:

    1. 9,3,4,#,# = 9,3,# Continue Reading
    2. 9,3,#,1,#,# = 9,3,#,# = 9,# Continue Reading
    3. 9, #2, #,6,#,# = 9,#,2,#,# = 9,#,# = #
1  PublicBoolean isvalidserialization (String preorder) {2stack<string> stack =NewStack<string>();3string[] arr = Preorder.split (",");4 5          for(inti =0; i < arr.length; i++) {6 Stack.push (Arr[i]);7              while(Stack.size () >=3&& stack.Get(Stack.size ()-1). Equals ("#")8&& stack.Get(Stack.size ()-2). Equals ("#") &&!stack.Get(Stack.size ()-3). Equals ("#")) {9Stack.remove (Stack.size ()-2);TenStack.remove (Stack.size ()-2); One             } A         } -  -         if(stack.size () = =1&& Stack.peek (). Equals ("#")) the             return true; -  -         return false; -}

Another solution: https://www.hrwhisper.me/leetcode-verify-preorder-serialization-of-a-binary-tree/

Looking at Dietpepsi's code, the idea is better than the one above me:

In a binary tree, if we consider null as leaves and then

  • All Non-null node provides 2 outdegree and 1 Indegree (2 children and 1 parent), except Root
  • All null node provides 0 outdegree and 1 indegree (0 child and 1 parent).

Suppose we try to build the this tree. During Building, we record the difference between out degree and in degree diff = outdegree - indegree . When the next node comes, we and decrease by diff 1, because the node provides a in degree. If the node is not null , we increase diff 2 by, because it provides-out degrees. If a serialization is correct, the diff should never be negative and diff would be the zero when finished.

From:https://leetcode.com/discuss/83824/7-lines-easy-java-solution

I translate here:

For binary trees, we also use empty places as leaf nodes (as in the title #), then there are

    • All non-empty nodes provide 2 degrees and 1 degrees (except for the root)
    • All empty nodes but provides 0 out and 1 degrees of penetration

When we traverse, we calculate the diff = outdegree–indegree. When a node appears, diff–1, because it provides an in degree, when the node is not #, diff+2 (provides two degrees) if the sequence is legal, then the Traverse process diff >=0 and the final result is 0.

1  PublicBoolean isvalidserialization (String preorder) {2string[] nodes = Preorder.split (",");3     intdiff =1;4      for(String node:nodes) {5         if(--diff <0)return false;6         if(!node.equals ("#")) diff + =2;7     }8     returndiff = =0;9}

Verify preorder serialization of a Binary Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.