C++ 5 lines recursive solution


  • 0
    B
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        bool isSymmetric(TreeNode* root) 
        {
            if(!root) return true;
            return sym(root->left, root->right);
        }
        
        bool sym(TreeNode* n1, TreeNode* n2)
        {
            if(!n1 && !n2) return true;
            if(!n1 || !n2 || n1->val != n2->val) return false;
            return sym(n1->left, n2->right) && sym(n1->right,n2->left);
        }
    };

  • 0
    J
    This post is deleted!

Log in to reply
 

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