leetcode Symmetric Tree

Symmetric Tree

题目详情:

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

解题方法1:

递归算法: 判断root是否为空,若为空则返回true,否则返回左子树和右子树是否对称。判断两棵树是否对称:若两棵树都为空,则对称,若两棵树有一个不为空,则不对称。若两棵树都不为空,他们的值不相同,则不对称,若两棵树都不为空,他们的值相同,则判断第一棵树的左子树和第二棵树的右子树是否对称,第一棵树的右子树和第二棵树的左子树是否对称,若两对树都对称,则两棵树对称,否则两棵树不对称。


代码详情:

/**
 * 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)
           return true;
       return isSametree(root->left,root->right);
    }
    bool isSametree(TreeNode* leftt, TreeNode* rightt) {
        if (leftt==NULL && rightt==NULL) {
            return true;
        } else if (leftt == NULL && rightt != NULL) {
            return false;
        } else if (leftt != NULL && rightt == NULL) {
            return false;
        } else {
            if (leftt->val != rightt->val) {
                return false;
            }
            if (isSametree(leftt->left,rightt->right) && isSametree(leftt->right,rightt->left) ) {
                return true;
            }
            return false;
        }
    }
};


解题方法2:

迭代算法:如果root为空,返回trrue。否则,root左子树进队列q1,右子树进队列q2。当q1,q2非空时,循环下面的过程:另temp1为q1.front(),temp2为q2.front,q1.pop(),q2.pop()。若temp1,temp2都为空,则continue。若一个不为空则返回false。若都不为空,但是value不相等,则返回false。若都不为空,且value相等,q1.push(temp1->left),q2.push(temp2->right),q1.push(temp1->right),q2.push(temp2->left)。


代码详情:

/**
 * 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) return true;
       queue<TreeNode*> q1, q2;
       q1.push(root->left);
       q2.push(root->right);
        TreeNode* temp1;
        TreeNode* temp2;
       while (!q1.empty() && !q2.empty()) {
           temp1 = q1.front();
           q1.pop();
           temp2 = q2.front();
           q2.pop();
           if (temp1 == NULL && temp2 == NULL) {
               continue;
           } else if (temp1 == NULL && temp2 != NULL) {
               return false;
           } else if (temp1 != NULL && temp2 == NULL) {
               return false;
           } else if (temp1 != NULL && temp2 != NULL) {
               if (temp1->val != temp2->val) {
                   return false;
               } else {
                   q1.push(temp1->left);
                   q2.push(temp2->right);
                   q1.push(temp1->right);
                   q2.push(temp2->left);
               }
           }
       }
        return true;
    }
};

扫描二维码关注公众号,回复: 1502394 查看本文章


猜你喜欢

转载自blog.csdn.net/weixin_40085482/article/details/78475477