leetcode || 101、Symmetric Tree

problem:

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.

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

Hide Tags
  Tree Depth-first Search
题意:判断一棵二叉树是否为镜像二叉树(结构对称,数字相等)

thinking:

(1)该题合适的解法是递归法,采用DFS思想

递归,保存左右两个节点,然后判断leftNode->left和rightNode->right,以及leftNode->right和rightNode->left。如此不断递归

(2)还想到另外一种非递归法:层序遍历法,子结点为NULL时代表数值0,从左往右层序遍历得到数组a,从右往左层序遍历得到数组b

如果a==b,则该二叉树是镜像的。但提交时显示:Status: 

Memory Limit Exceeded   


code:

递归法:

class Solution {
public:
    bool check(TreeNode *leftNode, TreeNode *rightNode)
    {
        if (leftNode == NULL && rightNode == NULL)
            return true;
            
        if (leftNode == NULL || rightNode == NULL)
            return false;
            
        return leftNode->val == rightNode->val && check(leftNode->left, rightNode->right) && 
            check(leftNode->right, rightNode->left);
    }
    
    bool isSymmetric(TreeNode *root) {
        if (root == NULL)
            return true;
            
        return check(root->left, root->right);
    }
};
层序遍历法: Status: 

Memory Limit Exceeded

class Solution {
private:
    vector<int> res1;
    vector<int> res2;
public:
    bool isSymmetric(TreeNode *root) {
        if(root==NULL)
            return true;
        res1.clear();
        res2.clear();
        level_walk_left(root);
        level_walk_right(root);
        if(res1==res2)
            return true;
        else
            return false;
    }
protected:
    void level_walk_left(TreeNode *root)
    {
        queue<TreeNode *> _queue;
        _queue.push(root);
        while(!_queue.empty())
        {
            TreeNode *tmp=_queue.front();
            if(tmp==NULL)
                res1.push_back(0);
            else
            {
                res1.push_back(tmp->val); 
                _queue.pop();
                if(tmp->left!=NULL)
                    _queue.push(tmp->left);
                else
                    _queue.push(NULL);
                if(tmp->right!=NULL)
                    _queue.push(tmp->right);
                else
                    _queue.push(NULL);
            }
        }
    }
    
    void level_walk_right(TreeNode *root)
    {
        queue<TreeNode *> _queue;
        _queue.push(root);
        while(!_queue.empty())
        {
            TreeNode *tmp=_queue.front();
            if(tmp==NULL)
                res2.push_back(0);
            else
            {
                res2.push_back(tmp->val); 
                _queue.pop();
                if(tmp->right!=NULL)
                    _queue.push(tmp->right);
                else
                    _queue.push(NULL);
                if(tmp->left!=NULL)
                    _queue.push(tmp->left);
                else
                    _queue.push(NULL);
            }
        }
    }
    
};



你可能感兴趣的:(LeetCode,二叉树,DFS,层序遍历,递归法)