June 28, 2022 leetcode daily check-in - 101. Symmetrical binary tree

1. Topic description and requirements

101. Symmetric Binary Tree - LeetCode

topic description

Given the root of a binary tree  root , check if it is axisymmetric.

example

Example 1:

输入:root = [1,2,2,3,4,4,3]
输出:true

 Example 2:

 

输入:root = [1,2,2,null,3,null,3]
输出:false

hint

  • The number of nodes in the tree is  [1, 1000] within the range
  • -100 <= Node.val <= 100

2. Problem-solving ideas

General idea:

To judge whether a binary tree is axisymmetric or not, there are two aspects, structure and node value. First, judge whether it is an empty tree. If it is an empty tree, you don’t need to judge, it must be axisymmetric; then if the tree is not empty, you need to consider whether the structure of the left subtree and right subtree of the root node is the same as the value of the corresponding node. The correspondence here is not like 100. The same tree is left to left, but the tree itself is compared. According to the axisymmetric graph, it should be that the left child of the left subtree corresponds to the right child of the right subtree, and the left subtree The right child of must correspond to the left child of the right subtree. Therefore, when making judgments in this step, you can write another function, and then call this function recursively to traverse the entire tree. First of all, if the left and right subtrees are all empty, it must be axisymmetric, and return true. If one is empty, it is not axisymmetric, and returns false. If there is a node whose value is not equal, it returns false directly. Continue to call the function to determine whether the left and right subtrees of the current node are the same as the left and right subtrees of sibling nodes.

Specific steps:

1. Determine whether the binary tree is empty
2. Determine whether the structure and value of the left and right subtrees are equal
3. Recursively call the function to traverse the binary tree
4. Return the result.

3. Specific code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool compare(struct TreeNode* left,struct TreeNode* right)
{
    if(left==NULL&&right==NULL)//如果二叉树根节点存在但是左右子树都为空则为轴对称,返回true
    {
        return true;
    }
    else if(left==NULL||right==NULL)//左右子树中有一颗为空树,另一颗不为空树,肯定不是轴对称,返回false
    {
        return false;
    }
    else if(left->val!=right->val)//如果左右子树中结点的值不相同则不为轴对称,返回false
    {
        return false;
    }
    else
    {   //如果上述比较都相等那么就对目前结点的左右子树继续进行比较,此时注意轴对称应当是左子树的左孩子与右子树的右孩子相同,左子树的右孩子与右子树的左孩子相同。通过递归来实现对每个结点的比较
        return compare(left->left,right->right)&&compare(left->right,right->left);
    }
}

bool isSymmetric(struct TreeNode* root){
    if(root==NULL)//如果二叉树为空树,则肯定是轴对称,返回true
       return true;
    else
    {
        return compare(root->left,root->right);
    }
}

Guess you like

Origin blog.csdn.net/m0_59800431/article/details/125494943