"Leetcode" 101. Symmetric Binary Tree

Language : C language
topic ideas :

  • The root node of the first layer does not need to be judged
  • Each recursion splits the tree into a left tree and a right tree
  • First judge whether the root nodes of the left tree and the right tree are the same, and then judge whether the left tree and the right tree are symmetrical
  • To determine whether two trees are symmetrical, you can use the previous idea to rewrite ``Leetcode'' 100. The same tree

Picture description :
Handwritten by myself
Code implementation :

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

bool _isSymmetric(struct TreeNode* t1, struct TreeNode* t2)//判断是否对称
{
    if(t1==NULL && t2==NULL)
    {
        return true;
    }
    if(t1==NULL || t2==NULL)
    {
        return false;
    }
    if(t1->val != t2->val)
    {
        return false;
    }
    return _isSymmetric(t1->left,t2->right) && _isSymmetric(t1->right,t2->left);
}

bool isSymmetric(struct TreeNode* root)
{
    if(root==NULL)
    {
        return true;
    }
    return _isSymmetric(root->left,root->right);
}

Guess you like

Origin blog.csdn.net/NanlinW/article/details/97274384