LeetCode: Symmetric Tree

思路:镜像树中,对于同一层处于镜像位置的两个节点A,B,A的左孩子需要与B的右孩子相同,A的右孩子需要与B的左孩子相同,依次递归这个过程即可

code:

class Solution {
public:
    bool isSym(TreeNode * left,TreeNode * right){
        if(left != NULL && right != NULL)
            return left->val == right->val && isSym(left->left,right->right) && isSym(left->right,right->left);
        if(left == NULL && right == NULL)
            return true;
        return false;
        
    }
    bool isSymmetric(TreeNode *root) {
        if(root == NULL)return true;
        return isSym(root->left,root->right);
    }
};


你可能感兴趣的:(LeetCode: Symmetric Tree)