Binary Tree OJ Question: LeetCode--101. Symmetrical Binary Tree

Friends and guys, we meet again. In this issue, I will explain to you the 144th binary tree OJ problem in LeetCode. If you have some inspiration after reading it, please leave your three links. I wish you a heart All wishes come true!

Data Structure and Algorithm Column: Data Structure and Algorithm

Personal homepage  : stackY,

C language column : C language: from entry to master

LeetCode--101. Symmetric binary tree:  https://leetcode.cn/problems/symmetric-tree/

Table of contents

1. Topic introduction

2. Example demonstration

3. Problem-solving ideas

Topic analysis:

Code demo:


1. Topic introduction

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

2. Example demonstration

3. Problem-solving ideas

Topic analysis:

To judge whether a binary tree is an axisymmetric binary tree, we must pay attention to a similar point here-judging whether the left and right subtrees of a binary tree are equal. These two problems have many similarities. We can draw a picture to see:

 

 It can be seen that the two are similar, the axisymmetric problem is converted into a binary tree, that is, the left root node of the left subtree in the left subtree of a root node and the right root node of the right subtree, and then convert them into root and left subtree , the subproblem of the right subtree.

According to the prompt, there is at least one node in the binary tree, so we can directly access the left and right subtrees, so for convenience, we can set a sub-function, and the judgment process is completed in this sub-function, so we only need to set the left and right sub-trees Just pass the tree to the sub-function, then in the sub-function, we must first judge whether the passed left and right sub-trees are empty, if they are all empty, the axisymmetric condition is satisfied, if only one is empty, then it is not satisfied, If they are not empty, then it is necessary to judge whether the two nodes are equal. If not, then the condition is not satisfied. If they are equal, continue to recursively traverse its left and right subtrees until the complete binary tree is traversed.

Code demo:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool _isSymmetric(struct TreeNode* left_root, struct TreeNode* right_root)
{
    //左右子树都为空,则表示对称
    if(left_root == NULL && right_root == NULL)
    {
        return true;
    }

    //若有一个不为空,则不对称
    if(left_root == NULL || right_root == NULL)
    {
        return false;
    }

    //都不为空则判断是否满足轴对称的特点
    if(left_root->val != right_root->val)
    {
        return false;
    }

    //递归继续遍历左右子树
    return _isSymmetric(left_root->left, right_root->right)
        && _isSymmetric(left_root->right, right_root->left);
} 

bool isSymmetric(struct TreeNode* root){

    //分装子函数来判断
    return _isSymmetric(root->left, root->right);
}

Friends and guys, the good times are always short-lived. This is the end of our sharing in this issue. Don’t forget to leave your precious trilogy after reading it. Thank you for your support!  

Guess you like

Origin blog.csdn.net/Yikefore/article/details/131522094