letecode [101] - Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
    1
   / \
  2   2
 / \ / \
3  4 4  3

 
But the following [1,2,2,null,3,null,3] is not:
    1
   / \
  2   2
   \   \
   3    3

Subject to the effect:
  Given a binary tree, it is determined that it is not symmetrical (symmetrical structure and values ​​are equal)
understanding:
  1. First determine the root node, if it is empty, then true; otherwise, if the child is left empty, was true, if the left and right child is not empty, it is determined whether or not equal, equal then determine if the two sub-trees symmetry; otherwise are false.
  2. For two subtrees:
    1) If root1 left child node and the root2 right child node is empty, root1 right child node and root2 left child is empty, symmetrical; root1 right child and root2 left child node that is not empty and the values ​​are equal, then recursively Comparative (root1 right child, root2 left child);
    2) If root1 left child node and the root2 when the right child node not empty, root1 right child node and root2 left child is empty, then the recursive comparison (root1 left child, root2 right sub); root1 right child and root2 left child node and non-null values ​​are equal, the comparison is continued recursively (the root1 left child, right child root2) and (the root1 right child, root2 left child);
    3) other conditions are false.
  Sort out these conditional statement, thinking it clear.
Code C ++:
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetricLeftAndRight(TreeNode* root1,TreeNode* root2){
        if(root1->left==NULL && root2->right==NULL){
            if(root1->right==NULL && root2->left==NULL){
                return true;
            }else if(root1->right!=NULL && root2->left!=NULL && root1->right->val==root2->left->val){
                return isSymmetricLeftAndRight(root1->right,root2->left);
            }else{
                return false;
            }
        }else if(root1->left!=NULL && root2->right!=NULL && root1->left->val==root2->right->val){
            if(root1->right==NULL && root2->left==NULL){
                return isSymmetricLeftAndRight(root1->left,root2->right);
            }else if(root1->right!=NULL && root2->left!=NULL && root1->right->val==root2->left->val){
                return isSymmetricLeftAndRight(root1->left,root2->right)&isSymmetricLeftAndRight(root1->right,root2->left);
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    
    bool isSymmetric(TreeNode* root) {
        if(root==NULL)
            return true;
        if(root->left==NULL && root->right==NULL)
            return true;
        if((root->left!=NULL && root->right==NULL)||(root->left==NULL && root->right!=NULL))
            return false;
        if(root->left->val == root->right->val){
            return isSymmetricLeftAndRight(root->left,root->right);
        }else
            return false;
    }
    
    
};

 

operation result:
   When executed with:  16 MS   memory consumption:  14.8 MB

 

Guess you like

Origin www.cnblogs.com/lpomeloz/p/10988641.html