Less than 10 lines recursion method in c++(4ms)


  • 2
    D
    class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
            if(!root) return true;
            return recursion_isSymmetric(root->left,root->right);
        }
        bool recursion_isSymmetric(TreeNode* left,TreeNode* right){
            if(!left&&!right) return true;
            else if(left&& right){
                if(left->val!=right->val) return false;
                return recursion_isSymmetric(left->left,right->right)&&recursion_isSymmetric(left->right,right->left);
            }
            else return false;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.