6 lines c++ solution


  • 1
    V
    class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
            if(!root)return true;
            return g(root->left,root->right);
        }
        bool g(TreeNode*n,TreeNode*p){
            if(!n&&!p) return true;
            if(!n||!p) return false;
            return n->val == p->val && g(n->left,p->right) && g(n->right,p->left);
        }
    
    };

  • 0
    L

    Beautiful solution, but I can‘t come up with it. ^_^


Log in to reply
 

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