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;
    }
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章