Leetcode's symmetric tree mirror

Source: Internet
Author: User
Tags bool

symmetric tree

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

For example, this binary the is symmetric:

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

But The following is not:

    1
   /\
  2   2
   \   \
   3    3
Recursive solution: Because of symmetry, each time the Zuozi of a node and its sibling node of the left subtree, 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: Thinking and recursion, where the use of two queues for processing, the children of the left child into the first queue, the right subtree into the second queue, each take the first element of the team to judge, faster than recursion faster than several times
struct TreeNode {int val;
     TreeNode *left;
     TreeNode *right;

TreeNode (int x): Val (x), left (null), right (null) {}}; Class Solution {Public:bool issymmetric (TreeNode *root) {//Judge 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 both empty if (!pnode1 | |!pnode2) return false;//only one is empty if (Pnode1->val!=
    		Pnode2->val) return false;//is 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 subtree} if (Leftqueue.size () > 0 ||
    	Rightqueue.size () > 0) return false;
    return true; }
};


Sword refers to an offer of the mirror tree problem: Enter a two-yuan lookup tree, the tree into its mirror, that is, in the converted two-yuan lookup tree, Zuozi node is greater than the right subtree node. The recursive and cyclic methods are used to complete the mirroring transformation of the tree.
Analysis: This is converted to a mirrored tree, which is similar to the judgment above, where you can either use recursion or use iterations to handle the left and right subtrees each time. Here and the difference in judgment is that the judge must be positioned to the location of each node's mirror node, and because the conversion does not need a one-step conversion, so the need for each node and adjacent nodes to exchange, relatively simple. For iterations, you can use queues, or you can use stacks, as follows:
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 from the above to judge the mirror tree
{
	if (!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 Two binary trees, write a function to check if they are or not.

Two binary trees are considered equal if they are structurally identical and the nodes the have 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;
    }
;


The iterative solution (similar to the above) is 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, the team is in the same order as
    	}
    	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.