The sword refers to the symmetrical binary tree of the offer

Topic description

Please implement a function to determine whether a binary tree is symmetric. Note that a binary tree is defined as symmetric if it is the same as its mirror image.

Problem solving ideas

write picture description here
When it comes to binary trees, there are three different ways of traversing binary trees, namely preorder traversal, inorder traversal and postorder traversal. In these three traversal algorithms, the left child node is first traversed before the right child node is traversed. To assess whether it is a symmetric tree, the best thing to think of is preorder traversal. If we are defining a traversal method similar to preorder traversal: that is, first traverse the parent node, then traverse the right child node, and finally traverse the left child Node. Take the above picture as an example: the preorder traversal in the first picture is 8657675, and the traversal according to the new definition is 8657675. The two sequences are the same. In the second picture, the preorder traversal is 8657975, and the traversal is 8957675 according to the new definition. These two sequences are different.
However, according to the above ideas, the following situation is not satisfied, as shown in the figure:
write picture description here

How to solve such a situation? In fact, we only need to consider the NULL pointer encountered when traversing. Take this picture as an example, the preorder traversal is [8 8 8 NULL NULL 8 NULL NULL 8 8 NULL NULL NULL], the new definition The traversal is [8 8 NULL 8 NULL NULL 8 8 NULL NULL 8 NULL NULL], the two sequences are different, so the problem has also been solved.

To sum up, we can judge whether the binary tree is symmetric by comparing the preorder traversal sequence of the binary tree and the newly defined similar preorder traversal sequence, adding the consideration of the NULL pointer. A binary tree is symmetric if the two sequences are the same. So write the code in this way as follows:

recursive version

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    bool isSymmetrical(TreeNode* pRoot)
    {
        if(pRoot == NULL)
            return true;
        return Symmetrical(pRoot->left,pRoot->right);
    }

    bool Symmetrical(TreeNode* t1, TreeNode* t2) {
        if(t1 == NULL && t2 == NULL) //如果左右子树都为空
            return true;     
        if(t1 != NULL && t2 != NULL) //如果左右子树都不为空
            return t1->val == t2->val && Symmetrical(t1->left,t2->right) && Symmetrical(t1->right, t2->left);
        return false;  //如果左右子树之一为空

    }

};

non-recursive version

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    bool isSymmetrical(TreeNode* pRoot)
    {
        if(pRoot == NULL) return true;
        queue<TreeNode*> q1,q2;
        TreeNode *left,*right;
        q1.push(pRoot->left);
        q2.push(pRoot->right);
        while(!q1.empty() and !q2.empty())
        {
            left = q1.front();
            q1.pop();
            right = q2.front();
            q2.pop();
            //两边都是空
            if(left == NULL && right == NULL)
                continue;
            //只有一边是空
            if(left == NULL || right == NULL)
                return false;
             if(left->val != right->val)
                return false;
            q1.push(left->left);
            q1.push(left->right);
            q2.push(right->right);
            q2.push(right->left);
        }       
        return true;        
    }

};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686891&siteId=291194637