leetcode#101 symmetric Tree

Source: Internet
Author: User
Tags null null

Original title Address

Some of the practice is to determine whether the sequence traversal series is a palindrome, at first I think it makes sense, but the thought of a moment not so simple.

For example, the following tree:

         1
/
1
/
1

The middle sequence traversal sequence is "111", although it is a palindrome, but the tree is obviously not symmetrical.

What if I counted the null? Or the tree above, and now it becomes this:

         1
/ \
1 NULL
/ \
1 NULL
/ \
NULL NULL

The sequence of sequential traversal is: "n1n1n1n", see, or wrong.

What if NULL is only computed for non-leaf nodes? Or the tree above, and now it becomes this:

         1
/ \
1 NULL
/ \
1 NULL

The middle sequence traversal sequence is: "11n1n", this example is no problem, but do not know whether it is right.

In short, the conclusion is not so "obvious", it is easy to think wrong. This method also has a disadvantage, that is, the entire number of traversal sequence must be saved, the spatial complexity is O (n), if the tree is large, the space cost is not small.

Therefore, the above method is not recommended.

The following two ways are recommended

Method I, hierarchical traversal of binary tree

Traverse the binary tree by layer, to determine whether the layer is a palindrome string, note the number of the node to save information

Code:

1 BOOLIssymmetric (TreeNode *root) {2   if(!root)return true;3 4Vector<pair<treenode *,int> >layer;5Layer.push_back (Pair<treenode *,int> (Root,0));6 7    while(!Layer.empty ()) {8Vector<pair<treenode *,int> >Nextlayer;9      for(inti =0; I < layer.size (); i++) {Ten       if(layer[i].first->Left ) OneNextlayer.push_back (Pair<treenode *,int> (Layer[i].first->left, I *2)); A       if(layer[i].first->Right ) -Nextlayer.push_back (Pair<treenode *,int> (Layer[i].first->right, I *2+1)); -     } the      for(inti =0, j = nextlayer.size ()-1; I <= J; i++, j--) -       if((Nextlayer[i].first->val! = nextlayer[j].first->val) -|| (Nextlayer[i].second + Nextlayer[j].second! = layer.size () *2-1)) -         return false; +Layer =Nextlayer; -   } +  A   return true; at}

Method II, recursive comparison (referenced from this blog post)

For any subtree, compare the root node: the left son's Saozi right son's right sub-tree is symmetrical, the left son's right subtree and the right son's Zuozi is symmetrical

Code:

1 BOOLIssymmetric (TreeNode *root) {2   returnRoot? Issymmetric (Root->left, root->right):true;3 }4 5 BOOLIssymmetric (TreeNode *left, TreeNode *Right ) {6   if(!left &&!right)return true; 7   if(!left | |!right)return false; 8   returnLeft->val = = Right->val && issymmetric (left->left, Right->right) && issymmetric (left-> Right, right->.Left );9}

Very ingenious.

leetcode#101 symmetric 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.