1. Topics
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
2. AnalysisTo determine whether a binary tree itself is mirror-symmetric, this problem can be translated into: whether the Zuozi of the two-fork tree and the right subtree are mirrored symmetrically. Once the problem has been transformed, there is a sense of déjà vu, and the previous article has just analyzed the question of judging whether two binary trees are equal, and the problem is simply to replace "equal" with "symmetry", the method is actually the same. Previous article: "Leetcode two fork tree equal judgment" same tree
The solution of the subject also has recursion and iteration two methods: (Set two fork tree left subtree for p, right subtree for q,p, q all point to the root of the left subtree) recursion: The value of P and Q is equal, and P's Zuozi and Q's right subtree symmetry, p right subtree and Q's left sub-tree symmetry
Iteration: Maintain a stack, the method and the previous article to determine whether the binary tree is equal, but, we put the node into the stack, the order is not p->left, Q->left, P->right, Q->right, but P->left, q ->right, P->right, Q->left, why. Because we want to judge the symmetry, so the p->left corresponds to the q->right,p->right corresponding to the q->left, their stacking order should be so.
3. Code
#递归
<span style= "FONT-SIZE:18PX;" >/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode (int x): Val (x), left (null), right (null) {}
*};
*/
class Solution {public
:
bool Issymmetric (TreeNode *root) {
if (!root) return true; The empty tree is a symmetric
return symmetric (root->left,root->right);
}
Private:
bool Symmetric (TreeNode *p,treenode *q)/ * Determine if two trees are symmetric/
{
if (!p &&!q) return True ; Both are empty
if (!p | | |!q) return false; Only one empty
return (p->val==q->val) &&symmetric (p->left,q->right) && symmetric (p-> Right,q->left);
/* tree P and Q symmetric conditions: P and q are the same values, and P's Zuozi and Q's right subtree symmetry, P's right subtree and Q's left sub-tree symmetric */
}
};</span>
#迭代
<span style= "FONT-SIZE:18PX;" >/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode (int x): Val (x), left (null), right (null) {}
*};
*/
class Solution {public
:
bool Issymmetric (TreeNode *root) {
if (!root) return true; The empty tree is symmetrical
stack<treenode *> s;
TreeNode *p=root->left,*q=root->right;
S.push (p);
S.push (q); Even an empty node can be pushed to the stack, and the stack is not empty. While
(!s.empty ())
{
p=s.top (); S.pop ();
Q=s.top (); S.pop ();
if (!p &&!q) continue; P, q are empty nodes
if (!p | | |!q) return false; There is an empty, asymmetric
if (p->val!=q->val) return false; Values are unequal, asymmetric
s.push (p->left); S.push (q->right);
S.push (p->right); S.push (q->left);
}
return true;
}
}; </span>