How To Check That Tree Is Symmetric

·

1 min read

LEETCODE 101

you will traverse tree in downward direction.

first check if tree is NULL if it is NULL then return true

Calculate its left & right subtree if

1.one of them is null & other is not then return false 2.both are null then return true 3.if value of (left.left == right.right)&&(left->right == right->left) then go for each left & right subtrees recursively.

CODE ->

int check(struct TreeNode* left,struct TreeNode* right){
    if(!left || !right){
        return (left == right);
    }

    if(left->val != right->val){
        return 0;
    }

    return check(left->left,right->right)&&check(left->right,right->left);
}

bool isSymmetric(struct TreeNode* root){
    if(root == NULL){
        return 1;
    }

    return check(root->left,root->right);
}