LeetCode:Symmetric Tree(镜像二叉树)

LeetCode:Symmetric Tree(镜像二叉树)


1、题目:
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


2、代码:
本题一波三折,自己AC的代码留个纪念:

/**
 * 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:
    void inOrder(TreeNode *root,vector<int> &v)
    {
        if(root)
        {
            if((!root->left)&&(root->right))
            {
               v.push_back(0); 
            }
            else
            {
                inOrder(root->left,v);
            }
            v.push_back(root->val);
            if((root->left)&&(!root->right))
            {
               v.push_back(0); 
            }
            else
            {
                inOrder(root->right,v);
            }
        }
    }

    bool isSymmetric(TreeNode* root) {
        if(!root)   return true;
        vector<int> v;
        inOrder(root,v);
        int len=v.size();
        for(int i=0,j=len-1;i!=j;++i,--j)
        {
            if(v[i]!=v[j])
            {
                return false;
            }
        }
        if(root->left&&root->right)
        {
            if(root->left->val!=root->right->val)
            {
                return false;
            }
        }
        return true;
    }
};

在一定程度上我钻了case的空子,将某些空子树用0代替,在末尾对两子树判断值相等。在AC后看了别人的代码,醍醐灌顶。镜像二叉树,你就判断镜像,老老实实判断两个子树,然后到判断子树的对应子树是否镜像。下面是递归解法:

/**
 * 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 isMirror(TreeNode *t1,TreeNode *t2)
    {
        if(t1==nullptr&&t2==nullptr)
        {
            return true;
        }
        if(t1==nullptr||t2==nullptr)
        {
            return false;
        }
        return (t1->val==t2->val)&&isMirror(t1->left,t2->right)&&isMirror(t1->right,t2->left);
    }
    bool isSymmetric(TreeNode* root) {
        if(!root)   return true;
        return isMirror(root->left,root->right);
    }
};

这是我根据他提供的java代码写的,顿时感觉水平差异无处不在,同志仍需努力。还有循环的方式,采用队列来记录节点,模式是一样的,都是比较对应子树,很巧妙的一直比较的都是对应子树。

/**
 * 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)   return true;
        queue<TreeNode*> q;
        q.push(root->left);
        q.push(root->right);
        while(!q.empty())
        {
            TreeNode *t1,*t2;
            t1=q.front();
            q.pop();
            t2=q.front();
            q.pop();
            if(t1==nullptr&&t2==nullptr)
            {
                continue;
            }
            if(t1==nullptr||t2==nullptr)
            {
                return false;
            }
            if(t1->val!=t2->val)
            {
                return false;
            }
            q.push(t1->left);
            q.push(t2->right);
            q.push(t1->right);
            q.push(t2->left);
        }
        return true;
    }
};

3、总结:
A、方法的寻找,紧扣问题本质,判断镜像。
B、对应子树判断,自己画一下,画到第3、4层就看懂了。
C、STL:容器、算法、迭代器。而C++标准库包括更多东西,例如一些宏定义等。
D、queue使用

C++ STL–stack/queue 的使用方法
http://www.cnblogs.com/mfryf/archive/2012/08/09/2629992.html

入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()。

E、定要将容器弄熟。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值