4ms && C++ && dfs


  • 2
    C
    class Solution {
    public:
        void dfs(bool &result, TreeNode* leftNode, TreeNode* rightNode) {
            if ((!leftNode != !rightNode) || (leftNode && leftNode->val != rightNode->val)) {
                result = false;
                return;
            }
            if (!leftNode) return;
            dfs(result, leftNode->left, rightNode->right);
            if (!result) return;
            dfs(result, leftNode->right, rightNode->left);
        }
        bool isSymmetric(TreeNode* root) {
            bool result = true;
            dfs(result, root, root);
            return result;
        }
    };

Log in to reply
 

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