LeetCode101——Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following is not:

    1
   / \
  2   2
   \   \
   3    3

Note: Bonus points if you could solve it both recursively and iteratively.

难度系数:

容易

实现

bool isSymmetric(TreeNode* lt, TreeNode* rt)
{
    if (lt == NULL && rt == NULL) {
        return true;
    } else if (lt == NULL || rt == NULL) {
        return false;
    } else if (lt->val != rt->val) {
        return false;
    }
    return isSymmetric(lt->left, rt->right) && isSymmetric(lt->right, rt->left);
}

bool isSymmetric(TreeNode *root) {
    if (root == NULL)
        return true;
    return isSymmetric(root->left, root->right);        
}

你可能感兴趣的:(LeetCode,C++,二叉树)