LeetCode刷题笔记--101. Symmetric Tree

101. Symmetric Tree

Easy

197145FavoriteShare

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

 

Note:
Bonus points if you could solve it both recursively and iteratively.

看了答案再写的,感觉真是简约多了:

/**
 * 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 comp(TreeNode* left, TreeNode* right)
    {
        if(!left&&!right)return true;
        if((!left&&right)||(left&&!right)||(left->val!=right->val))return false;
        return comp(left->left,right->right)&&comp(left->right,right->left);//此处注意,对比左左和右右,不要写错了。
    }
    
    bool isSymmetric(TreeNode* root) {
        if(!root)return true;
        return comp(root->left,root->right);
    }
    
    
};

AC的代码如下,没看答案写的,还调试了半天。感觉写的太多了点,但是通过了。出过问题的地方都标记到代码上了。

思路是:根不用检查
root->next需要完全一样
第三层,检查左左==右右  左右==右左
第四层,把该层的元素取出,分别放入两个queue中

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
#include <iostream>
#include <math.h>
using namespace std;
class Solution {
public:
    bool comp(queue<TreeNode*> L,queue<TreeNode*> R,int level)
    {
        int tlevel=level;
        level=0;
        bool t=true;//此处要检查每一次对比的结果,如果有一次不相等,t=false,如果每次都相等,t=true
        while(tlevel>0)
        {
            TreeNode* tempL=L.front();
            L.pop();
            TreeNode* tempR=R.front();
            R.pop();
            if(tempL==NULL&&tempR==NULL){}
            else if(tempL==NULL||tempR==NULL)t=false;
            else if(tempL->val!=tempR->val)t=false;
            else
            {
                if(tempL->left==NULL&&tempR->right==NULL&&tempL->right==NULL&&tempR->left==NULL){}
                else if(tempL->left==NULL&&tempR->right==NULL&&tempL->right!=NULL&&tempR->left!=NULL)
                {
                    L.push(tempL->right);
                    R.push(tempR->left);
                    level++;
                }
                else if(tempL->left!=NULL&&tempR->right!=NULL&&tempL->right==NULL&&tempR->left==NULL)
                {
                    L.push(tempL->left);
                    R.push(tempR->right);
                    level++;
                }
                else if(tempL->left!=NULL&&tempR->right!=NULL&&tempL->right!=NULL&&tempR->left!=NULL)
                {
                    L.push(tempL->left);
                    L.push(tempL->right);
                    R.push(tempR->right);
                    R.push(tempR->left);
                    level+=2;
                }
                else return false;
                
            }
            tlevel--;
            if(tlevel==0&&!L.size()&&t)return true;//此处检查是否所有的值都检查完了,即都到叶子节点了,此时如果t都是true,说明对比下来都一样的,返回true
            else if(tlevel==0&&!L.size()&&!t)return false;//所有的值都检查完了,存在至少1个不同的值,返回false
        }
        //level=std::pow(2,level+1)/2;
        if(!t)return false;
        else{return comp(L,R,level);}
        
    }
    
    bool isSymmetric(TreeNode* root) {
        if(root==NULL)return true;
        if(root->left==NULL&&root->right==NULL)return true;
        if(root->left==NULL||root->right==NULL)return false;
        int level=1;
        queue<TreeNode*> L;
        queue<TreeNode*> R;
        L.push(root->left);
        R.push(root->right);
        return comp(L,R,level);
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值