首页 > 代码库 > leetcode-Symmetric Tree 对称树

leetcode-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

方法一:

层次遍历是最直观的方法。对数进行层次遍历,记录每一层的节点,然后对每一层的value组成的字符串判断是不是对称串。算法的时间复杂度为O(nlgn),非最优,侥幸AC。

 1 class Solution {
 2 public:
 3     bool isSymmetric(TreeNode *root) {
 4         if(!root)
 5             return true;
 6         if(root->left==NULL && root->right!=NULL || root->left!=NULL && root->right==NULL)
 7             return false;
 8         if(!root->left && !root->right)
 9             return true;
10         mm.insert(make_pair(root->left,root->right));
11         return judge();
12     }
13 private:
14     multimap<TreeNode*,TreeNode*> mm;  //存放每层的节点,将对称位置上的一对节点存在一个key-value对里面
15 
16     bool judge(){
17         if(mm.empty())
18             return true;
19         multimap<TreeNode*,TreeNode*> tmp(mm);
20         mm.clear();
21         for(multimap<TreeNode*,TreeNode*>::iterator it=tmp.begin();it!=tmp.end();++it){
22             if(it->first->val!=it->second->val)
23                 return false;
24             if(it->first->left && !it->second->right)
25                 return false;
26             if(!it->first->left && it->second->right)
27                 return false;
28             if(it->first->right && !it->second->left)
29                 return false;
30             if(!it->first->right && it->second->left)
31                 return false;
32             if(it->first->right && it->second->left)
33                 mm.insert(make_pair(it->first->right,it->second->left));
34             if(it->first->left && it->second->right)
35                 mm.insert(make_pair(it->first->left,it->second->right));
36         }
37         return judge();  //递归到树的下一层
38     }
39 };

 

方法二:

        不采用层次遍历。直接比较对称位置:left的right和right的left比较,left的left和right的right比较。时间复杂度O(n)下面给出递归和非递归两个版本:

1、递归版本

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSymmetric(TreeNode *root) {
13         if(root==NULL) return true;
14         return isSymmetric(root->left,root->right);
15     }
16     bool isSymmetric(TreeNode *left, TreeNode *right){
17         if(left==NULL&&right==NULL) return true;
18         if(left==NULL||right==NULL) return false;
19         if(left->val!=right->val) return false;
20         return isSymmetric(left->left,right->right)&&isSymmetric(left->right,right->left);
21     }
22 };

 

2、非递归版本

 1 class Solution {
 2 public:
 3     bool isSymmetric (TreeNode* root) {
 4         if (!root) return true;
 5         stack<TreeNode*> s;
 6         s.push(root->left);
 7         s.push(root->right);
 8         while (!s.empty ()) {
 9             auto p = s.top (); s.pop();
10             auto q = s.top (); s.pop();
11             if (!p && !q) continue;
12             if (!p || !q) return false;
13             if (p->val != q->val) return false;
14             s.push(p->left);
15             s.push(q->right);
16             s.push(p->right);
17             s.push(q->left);
18         }
19         return true;
20     }
21 };

 

leetcode-Symmetric Tree 对称树