Leetcode's symmetric tree mirror

Source: Internet
Author: User
Tags bool

Symmetric Tree

Given a binary tree, check whether it is a mirror of the itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
   /\
  2   2
 /\/\
3  4 4  3

But the following are not:

    1
   /\
  2   2
   \   \
   3    3
Recursive solution: Because of symmetry, each time the Zuozi of a node and its sibling nodes of the left subtree comparison, and then recursive processing can be.
struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode (int x): Val (x), left (null), right (null) {}
};

Class Solution {public
:
bool Issymmetricrecursive (treenode* root1,treenode* root2)
{
	if (root1 = = Null && ROOT2 = = null) return true;
	if (ROOT1 = = NULL | | root2 = = NULL) return false;
	return (Root1->val = root2->val) && issymmetricrecursive (root1->left,root2->right) && Issymmetricrecursive (Root1->right,root2->left);

}
    BOOL Issymmetric (TreeNode *root) {
        if (root = NULL) return true;
	Return issymmetricrecursive (root->left,root->right);
    }
;


Iterative method: The idea and recursion, the use of two queues for processing, the left child tree in the first queue, the right sub-tree into the second queue, each time the first element of the team to judge, faster than recursion several times faster
struct TreeNode {int val;
     TreeNode *left;
     TreeNode *right;

TreeNode (int x): Val (x), left (null), right (null) {}}; Class Solution {Public:bool issymmetric (TreeNode *root) {//judgment mirror Tree and build mirror tree different if (!root | |!root->left &&
    	!root->right) return true;
    	Queue<treenode*> Leftqueue,rightqueue;
    	Leftqueue.push (Root->left);
    	Rightqueue.push (Root->right);
    		while (Leftqueue.size () > 0 && rightqueue.size () > 0) {treenode* pNode1 = Leftqueue.front ();
    		Leftqueue.pop ();
    		treenode* PNode2 = Rightqueue.front ();
    		Rightqueue.pop (); if (!pnode1 &&!pnode2) continue;//are empty if (!pnode1 | |!pnode2) Return false;//There is only one empty if (pnode1->val! =
    		Pnode2->val) return false;//are not empty Leftqueue.push (Pnode1->left), Leftqueue.push (pnode1->right);//First Queue Advanced 左子树  Rightqueue.push (Pnode2->right), Rightqueue.push (pnode2->left);//second queue advanced right sub-tree} if (leftqueue.size () > 0 ||
    	Rightqueue.size () > 0) return false;
    return true; }
};


Sword refers to the mirror tree title of offer: Enter a two-dollar lookup tree to convert the tree to its image, that is, in the converted two-tuple lookup tree, the nodes of the Zuozi are larger than the nodes of the right subtree. Recursive and cyclic methods are used to complete the image transformation of the tree.
Analysis: Here is the conversion to the mirror tree, the idea is similar to the above judgment, each time the left and right sub-tree processing, you can use recursion, you can also use the iteration. The difference here and the judge is that the judgment must be positioned to the location of each node's mirror node, and the conversion is not required to complete the conversion, so the need for each node and adjacent nodes to exchange, relatively simple. For iterations, you can use a queue, or you can use a stack, with the following code:
struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode (int x): Val (x), left (null), right (null) {}
};

void symmetrictreerecursive (treenode* root)//recursive, note the difference between the mirror tree and the above (
	!root) return;
	treenode* tmp = root-left;
	Root-left = root->right;
	Root->right = tmp;
	Symmetrictreerecursive (root->left);
	Symmetrictreerecursive (root->right);
}

void symmetrictreeiterative (treenode* root)//iteration
{
	if (!root) return;
	stack<treenode*> s;
	S.push (root);
	while (!s.empty ())
	{
		treenode* pcur = S.top ();
		S.pop ();
		treenode* tmp = pcur->left;
		Pcur->left = pcur->right;
		Pcur->right = tmp;
		if (pcur->left) S.push (pcur->left);
		if (pcur->right) S.push (pcur->right);
	}
}

same Tree

Given binary trees, write a function to check if they is equal or not.

The binary trees is considered equal if they is structurally identical and the nodes has the same value.

Recursive solution

struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode (int x): Val (x), left (null), right (null) {}
};

Class Solution {public
:
    bool Issametree (TreeNode *p, TreeNode *q) {
        if (p = = NULL && q = = null) return true;
	if (p = = NULL | | q = NULL) return false;
	if (P->val = = Q->val) return Issametree (p->left,q->left) && issametree (p->right,q->right);
	else return false;
    }
};


Iterative solution (similar to the above), 7 times times higher than the recursive algorithm
struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode (int x): Val (x), left (null), right (null) {}
};

Class Solution {public
:
    bool Issametree (TreeNode *p, TreeNode *q) {
    	if (!p &&!q) return true;
    	if (!p | |!q) return false;
    	Queue<treenode*> queue1,queue2;
    	Queue1.push (P), Queue2.push (q);
    	while (Queue1.size () >0 && queue2.size () >0)
    	{
    		treenode* pNode1 = Queue1.front (), *pnode2 = Queue2.front ();
    		Queue1.pop (), Queue2.pop ();
    		if (!pnode1 &&!pnode2) continue;
    		if (!pnode1 | |!pnode2) return false;
    		if (pnode1->val! = Pnode2->val) return false;
    		Queue1.push (Pnode1->left), Queue1.push (pnode1->right);
    		Queue2.push (Pnode2->left), Queue2.push (pnode2->right);//Because the same tree is judged, so enter the queue in the same order
    	}
    	if (Queue1.size () >0 | | Queue2.size () >0) return false;
    	return true;
    }
};



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.