Leetcode 101 Solution

This article provides solution to leetcode question 101 (symmetric-tree)

https://leetcode.com/problems/symmetric-tree

Solution

/**
 * 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* root1, TreeNode* root2)
    {
        if (root1 == NULL && root2 == NULL)
            return true;

        if (root1 == NULL || root2 == NULL)
            return false;

        if (root1->val != root2->val)
            return false;

        return isSymmetric(root1->left, root2->right) && isSymmetric(root1->right, root2->left);
    }

    bool isSymmetric(TreeNode* root) {
        return isSymmetric(root, root);
    }
};