Binary tree OJ question 2

Today we will take a look at a question to determine whether a tree is a symmetric binary tree. We will try to solve the question 101.

https://leetcode.cn/problems/symmetric-tree/

 Let’s first analyze this question. To determine whether this question is symmetrical, we first need to determine whether the left and right subtrees of the root node of the tree are symmetrical. So our comparison object is the left and right subtrees of the root node, then we might as well Write a function my_isSymmetric yourself, the parameters are

bool my_isSymmetric(struct TreeNode* leftroot,struct TreeNode* rightroot), we use this function to determine whether the left and right subtrees of this tree are symmetrical. First, we have to determine if

Are the left and right subtrees both empty trees? That is to say, this tree has only one root node, so it is still symmetrical, that is

if(leftroot==NULL&&rightroot==NULL)
    {
        return true;
    }

We have judged the situation when both the left and right subtrees are empty. Now, how to judge the situation when one side is empty? It is definitely asymmetric, that is

if(leftroot==NULL||rightroot==NULL)
    {
        return false;
    }

Some people may wonder why the connection symbol here is ||. Note that the premise for the program to reach this point is that the left and right subtrees of this tree are not empty, that is, both the left and right subtrees will not be empty at the same time, so the || symbol is used if leftroot ==NULL will not go to the back rightroot==NULL. If leftroot!=NULL, go to the back to determine whether right==NULL is empty. If one side of both sides is empty, the tree will definitely be asymmetrical and return false;

Next, if the program goes through the above step, it proves that neither the left nor right subtree is empty. Then we only need to judge whether the val of leftroot and the val of rightroot are equal. If they are not equal, return false, that is

if(leftroot->val!=rightroot->val)
    {
        return false;
    }

At this time, the program has not returned, that is, the above steps have been successfully passed, then recursively determine whether the left subtree of leftroot and the right subtree of righttoor and the right subtree of leftroot and the left subtree of right are equal.

Right now

 return my_isSymmetric(leftroot->left,rightroot->right)&&my_isSymmetric(leftroot->right,rightroot->left);

This function has been encapsulated here. We only need to call our own my_isSymmetric under the given isSymmetric, that is

 return my_isSymmetric(root->left,root->right);

 Complete code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool my_isSymmetric(struct TreeNode* leftroot,struct TreeNode* rightroot)
{
    if(leftroot==NULL&&rightroot==NULL)//判断左右子树是否都为空
    {
        return true;
    }
    if(leftroot==NULL||rightroot==NULL)//判断是由有一边为空
    {
        return false;
    }
    if(leftroot->val!=rightroot->val)//判断左右子树val是否相等
    {
        return false;
    }
    return my_isSymmetric(leftroot->left,rightroot->right)&&my_isSymmetric(leftroot->right,rightroot->left);//左子树的左子树和右子树的右子树比较,左子树的右子树和右子树的左子树比较,二者必须同时满足;

} 
bool isSymmetric(struct TreeNode* root) {
    return my_isSymmetric(root->left,root->right);
}

Guess you like

Origin blog.csdn.net/weixin_67131528/article/details/134691685