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.

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

solution 1: 用dfs中序遍历tree,将遍历出来的node的value和height存在数组中,如果数组是回文的,那么就是对称的。

solution 2: 用递归,转换为子树对称问题。

solution 3: 将solution 2改成非递归的方案, 用两个stack

solution 1: 

#include <stack>
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root == NULL || (root->left == NULL && root->right == NULL))
            return true;
        stack<TreeNode*> elements;
        stack<int> height;//store each element's level.
        vector<int> values;//store the dfs elements. detect if huiwen.
        vector<int> levels;//store the dfs element's level, detect if equal.
        elements.push(root);
        height.push(1);
        bool left = true;//left subtree can be visit.
        int level = 1;
        //dfs
        while(!elements.empty())
        {
            TreeNode* leftNode = elements.top();
            //iterate left
            while(leftNode->left && left)
            {
                leftNode = leftNode->left;
                elements.push(leftNode);
                level++;
                height.push(level);
            }
            //visit node.
            TreeNode* top = elements.top();
            values.push_back(top->val);
            levels.push_back(height.top());
            level = height.top();
            height.pop();
            elements.pop();
            
            left = false;
            //push right.
            if(top->right)
            {
                elements.push(top->right);
                left = true;
                level++;
                height.push(level);
            }
        }
        for(int i = 0 ; i < values.size()/2; i++)
        {
            if(values[i] != values[values.size() - i - 1] || levels[i] != levels[levels.size() - i -1])
                return false;
        }
        return true;
    }
};


solution 2:

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(!root)return true;
        return g(root->left,root->right);
    }
    bool g(TreeNode*n,TreeNode*p){
        if(!n&&!p) return true;
        if(!n||!p) return false;
        return n->val == p->val && g(n->left,p->right) && g(n->right,p->left);
    }

};

solution 3:

bool isSymmetric(TreeNode* root) {
         stack<TreeNode*> s1;
         stack<TreeNode*> s2;
         if(root == NULL)   return true;
         s1.push(root); s2.push(root);
         while(!s1.empty() && !s2.empty()){
             TreeNode* n1 = s1.top();
             TreeNode* n2 = s2.top();
             s1.pop();
             s2.pop();
             if(n1->val != n2->val)   return false;
             if(n1->left != NULL && n2->right != NULL){
                 s1.push(n1->left);
                 s2.push(n2->right);
             }
             if(n1->right != NULL && n2->left != NULL){
                 s1.push(n1->right);
                 s2.push(n2->left);
             }else if((n1->left == NULL && n2->right == NULL) || (n1->right == NULL && n2->left ==NULL))
                continue;
              else return false;
         }
         return true;
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值