My short and easy C++ Accepted code in 3ms with recursion solution


  • 0
    S
    class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
    		return Equal(root, root);
        }
    
    	bool Equal(TreeNode* p, TreeNode* q)
    	{
    		if (!p && !q)
    		{
    			return true;
    		}
    		else if (!p && q || p && !q || p->val != q->val)
    		{
    			return false;
    		}
    		else
    		{
    			return (Equal(p->left, q->right) && Equal(p->right, q->left));
    		}
    	}
    };
    

Log in to reply
 

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