101. 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
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
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
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
解题思路1:遍历的时候就进行判断
c++代码1:
/**
* 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 *p,TreeNode *q){
if(p==NULL&&q==NULL)return true;//当前根节点的左右孩子为空
else if((p==NULL&&q!=NULL)||(p!=NULL&&q==NULL))return false;//当前根节点的左右孩子不同时为空
//当前根节点的左右孩子不为空 递归遍历该左孩子的左孩子与右孩子的右孩子;该左孩子的右孩子与右孩子的左孩子
else if(p->val==q->val)return isMirror(p->left,q->right)&&isMirror(p->right,q->left);
else return false;//不相等
}
bool isSymmetric(TreeNode* root) {
if(root==NULL)return true;//根节点为空
else return isMirror(root->left,root->right);
}
};
(1)通过队列:一个队列保存从最初的根节点的左子树;另一个队列保存从最初的根节点的右子树
(2)然后一一比较两个队列中的元素的值(只要不相等 就不是镜像树)
(3)注意进队列的顺序
c++代码2:
/**
* 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*> leftTree,rightTree;//队列
TreeNode *leftchild,*rightchild;
leftTree.push(root->left);
rightTree.push(root->right);
while(!leftTree.empty()&&!rightTree.empty()){
leftchild=leftTree.front();
leftTree.pop();
rightchild=rightTree.front();
rightTree.pop();
if(leftchild==NULL&&rightchild==NULL)continue;
else if(leftchild==NULL||rightchild==NULL)return false;
else if(leftchild->val!=rightchild->val)return false;
leftTree.push(leftchild->left);//进队列注意先进入左孩子的左孩子
leftTree.push(leftchild->right);//进队列注意后进入左孩子的右孩子
rightTree.push(rightchild->right);//进队列注意先进入右孩子的右孩子
rightTree.push(rightchild->left);//进队列注意后进入右孩子的左孩子
}
return true;
}
};
解题思路3:
(1)通过栈:通过比较栈顶和此栈顶两个元素的值(只要不相等 就不是镜像树)
(2)注意进栈的顺序
c++代码3:
/**
* 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;//根节点为空
stack<TreeNode*> S;//栈
TreeNode *leftchild,*rightchild;
S.push(root->left);
S.push(root->right);
while(!S.empty()){
leftchild=S.top();
S.pop();
rightchild=S.top();
S.pop();
if(leftchild==NULL&&rightchild==NULL)continue;
else if(leftchild==NULL||rightchild==NULL)return false;
else if(leftchild->val!=rightchild->val)return false;
S.push(leftchild->left);//进栈注意先进入左孩子的左孩子
S.push(rightchild->right);//进栈注意先进入右孩子的右孩子
S.push(leftchild->right);//进栈注意先进入左孩子的右孩子
S.push(rightchild->left);//进栈注意先进入右孩子的左孩子
}
return true;
}
};