LeetCode——symmetric tree 对称树

LeetCode——symmetric tree 对称树

题目描述:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
Note:
Bonus points if you could solve it both recursively and iteratively.
题目分析:
递归实现:很简单,只要在判断两棵树是否相等的代码上简单修改即可
非递归实现:我们知道前序+中序遍历序列(或者后序+中序遍历序列)可以唯一确定一颗二叉树,所此处可以将右子树的前序遍历改成(根->右->左),相应的将中序改为(右->根->左),将左右子树的序列相比较,如果都相同说明是对称树。

递归解法:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
   bool isMirrorTree(TreeNode *p, TreeNode *q) {
        if(p==NULL&&q==NULL)
            return true;
        else if(p==NULL&&q!=NULL)
            return false;
        else if(p!=NULL&&q==NULL)
            return false;
        else if(p->val!=q->val)
            return false;
        else 
          return isMirrorTree(p->left,q->right)
               &&isMirrorTree(p->right,q->left);
    }
        
public:
    bool isSymmetric(TreeNode *root) {
        if(root==NULL )
            return true;
        else 
            return isMirrorTree(root->left, root->right);
    }   
};

非递归解法:

class Solution {
private:
    vector<int> Preorder1,Preorder2;
    vector<int> Inorder1,Inorder2;
public:
    bool isSymmetric(TreeNode *root) {
        if(root==NULL)
           return true;
        Root_Left_Right(root->left);
        Root_Right_Left(root->right);
        Left_Root_Right(root->left);
        Right_Root_Left(root->right);
        return Yes();
    }
private:
   void Root_Left_Right(TreeNode *root)
   {
       if(root==NULL)
           return;
       Preorder1.push_back(root->val);
       Root_Left_Right(root->left);
       Root_Left_Right(root->right);
   }
    void Root_Right_Left(TreeNode *root)
    {
         if(root==NULL)
           return;
       Preorder2.push_back(root->val);
       Root_Right_Left(root->right);
       Root_Right_Left(root->left);
    }
    void Left_Root_Right(TreeNode *root)
    {
        if(root==NULL)
           return;       
       Left_Root_Right(root->left);
       Inorder1.push_back(root->val);
       Left_Root_Right(root->right);
    }
    void Right_Root_Left(TreeNode *root)
    {
        if(root==NULL)
           return;       
       Right_Root_Left(root->right);
       Inorder2.push_back(root->val);
       Right_Root_Left(root->left);
    }
    bool Yes()//判断是否前序和中序都相同
    {
        if(Preorder1.size()!=Preorder2.size()||Inorder1.size()!=Inorder2.size())
            return false;
        for(int i=0;i!=Inorder1.size();i++)
            if(Inorder1[i]!=Inorder2[i])
                return false;
        for(int i=0;i!=Preorder1.size();i++)
            if(Preorder1[i]!=Preorder2[i])
                return false;
        return true;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值