solution_101

LeetCode Problem 101 Description

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

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

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

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3

Solution

We can solve this problem with iterative method and recursive method either and I will show my code about them. However, the most important rule of both the methods is that for the left child and the right child of the ancestor there must be left->left->val = right->right->val and left->right->val = right->left->val so that the tree is symmetric. What we need to do is to compare the preceding two equations repeatedly.

Recursive method

As we can see, I set an additional method called treeComapre to find whether the left child is the same as the right child. And it will recursively compare the pair of nodes.

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct TreeNode {
    int val;
    TreeNode * left;
    TreeNode * right;
    TreeNode (int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    bool isSymmetric (TreeNode * root);
};

//Using the recursive method to achieve it
bool treeCompare (TreeNode * left, TreeNode * right) {
    if (left == NULL && right == NULL) {
        return true;
    }
    else if (left == NULL || right == NULL) {
        return false;
    }
    else {
        if (left->val != right->val) {
            return false;
        }
    }
    return true && treeCompare(left->left, right->right) && treeCompare(left->right, right->left);
}

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

Iterative method

Just like the recursive method, what we need to do is to find whether the corresponding nodes are symmetric with the two important rules. However, we should compare the direct child of the root first since the following loop of comparision is based on two tree nodes. And we use the BFS algorithm to achieve this. Using a FIFO queue to store the piars of nodes.

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct TreeNode {
    int val;
    TreeNode * left;
    TreeNode * right;
    TreeNode (int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    bool isSymmetric (TreeNode * root);
};

//Using the iterative method to compare them, and it is BFS
bool Solution::isSymmetric (TreeNode * root) {
    queue<TreeNode *> temp;
    //Compare the direct child of the root
    if (root == NULL) {
        return true;
    }
    else if (root->left == NULL && root->right == NULL) {
        return true;
    }
    else if (root->left == NULL || root->right == NULL) {
        return false;
    }
    else {
        if (root->left->val != root->right->val) {
            return false;
        }
    }
    TreeNode * l = root->left, * r = root->right;
    temp.push(l);
    temp.push(r);
    //Compare the rest nodes.
    while (!temp.empty()) {
        l = temp.front();
        temp.pop();
        r = temp.front();
        temp.pop();
        if (l == NULL && r == NULL){
            continue;
        }
        else if (l == NULL || r == NULL) {
            return false;
        }
        else {
            if (l->val != r->val) {
                return false;
            }
        }
        temp.push(l->left);
        temp.push(r->right);
        temp.push(l->right);
        temp.push(r->left);
    }
    return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值