My C++ Accepted code in 3ms with reverse


  • 0
    S

    class Solution {
    public:

    bool isSameTree(TreeNode* p, TreeNode* q) {
    	if (!p && !q) return true;
    	if ((p && q) && p->val == q->val)
    		return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    	return false;
    }
    
    void reverseBinaryTree(TreeNode*& root) {
    	if (!root) return;
    	std::swap(root->left, root->right);
    	reverseBinaryTree(root->left);
    	reverseBinaryTree(root->right);
    }
    
    bool isSymmetric(TreeNode* root) {
    	if (!root ) return true;
    	if (root && !root->left && !root->right) return true;
    	if (root->right && root->left && root->right->val == root->left->val) {
    		reverseBinaryTree(root->left);
    		return isSameTree(root->left, root->right);
    	}
    	return false;
    }
    

    };


Log in to reply
 

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