LeetCode - 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.

http://oj.leetcode.com/problems/symmetric-tree/


Solution:

Recursive way is kind of like same tree. We can check if left subtree is mirror of right subtree, which is compare root1->left and root2->right, root1->right and root2->left.

Iterative way  is to check every level. Attention that inorder traversal is not correct in that input {1, 2, 3, 3, #, 2, #}. The result is {3, 2, 1, 2, 3} but obviously it is not a symmetric tree.

https://github.com/starcroce/leetcode/blob/master/symmetric_tree.cpp

// 28 ms for 190 cases
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if(root == NULL) {
            return true;
        }
        return isSym(root->left, root->right);
    }
    
    bool isSym(TreeNode *left, TreeNode *right) {
        if(left == NULL && right == NULL) {
            return true;
        }
        if(left == NULL || right == NULL) {
            return false;
        }
        if(left->val != right->val) {
            return false;
        }
        if(!isSym(left->right, right->left)) {
            return false;
        }
        if(!isSym(left->left, right->right)) {
            return false;
        }
        return true;
    }
};
// 48 ms for 191 test cases
// iterative way, check every level
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if(root == NULL) {
            return true;
        }
        vector<TreeNode *> preLevel;
        preLevel.push_back(root);
        while(!preLevel.empty()) {
            vector<TreeNode *> curLevel;
            // push child node to curLevel
            while(!preLevel.empty()) {
                TreeNode *tmp = preLevel.back();
                preLevel.pop_back();
                if(tmp == NULL) {
                    continue;
                }
                curLevel.push_back(tmp->left);
                curLevel.push_back(tmp->right);
            }
            int start = 0, end = curLevel.size() - 1;
            // check the val in curLevel
            while(start < end) {
                TreeNode *leftNode = curLevel[start];
                TreeNode *rightNode = curLevel[end];
                int leftVal, rightVal;
                if(leftNode == NULL) {
                    leftVal = -1;
                }
                else {
                    leftVal = leftNode->val;
                }
                if(rightNode == NULL) {
                    rightVal = -1;
                }
                else {
                    rightVal = rightNode->val;
                }
                if(leftVal != rightVal) {
                    return false;
                }
                start++;
                end--;
            }
            // replace preLevel with curLevel
            preLevel = curLevel;
        }
        return true;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值