Leetcode 331. Verify preorder serialization of a Binary Tree

Source: Internet
Author: User

Transmission Door

331. Verify preorder serialization of a Binary TreeMy SubmissionsQuestionEditorial SolutionTotal accepted:10790 Total submissions:34071 difficulty:medium

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

Test instructions: Judging the given string is not a valid pre-order traversal idea: with a stack, if a node's Saozi right subtree is true, the node returns true False if the condition: 1. A node has been traversed by both the left and right subtrees, followed by the son of the node (e.g. 1,#,#,#) 2. The root node is the end of the access, followed by other nodes (#,1 or 1,#,#,1)
150/150 Test cases passed. status:accepted
Runtime:8 ms
1 classSolution {2  Public:3     BOOLIsvalidserialization (stringpreorder) {4         intL =preorder.length ();5         if(preorder[0] =='#'){6             if(L = =1)return true;7             Else return false;8         }9         inti =0;Tenstack<int>s; OneS.push (0); A          while(I<l && preorder[i]! =',') i++; -i++; -         intte; the          while(I <l) -         { -             if(Preorder[i] = ='#'){ -                 if(S.empty () = =1)return false; +                  while(!s.empty ()) -                 { +Te =s.top (); A                     if(Te = =2)return false; at S.pop (); -S.push (Te +1); -Te =s.top (); -                     if(Te = =1){ -                          Break; -                     } in                     if(Te = =2 ){ - S.pop (); to                     } +                 } -                 if(S.empty () = =1&& I < L-1)return false; the             } *             Else{ $S.push (0);Panax Notoginseng             } -              while(I < L && Preorder[i]! =',') i++; thei++; +         } A         if(s.size () = =0)return true; the         return false; +     } -};

Leetcode 331. 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.