###### New coder, Step further

winter is coming...

###### 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 [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

#### 方法一：递归

bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
return helper(root->left, root->right);
}
bool helper(TreeNode* p, TreeNode *q) {
if (p == NULL || q == NULL)
return (p == NULL && q == NULL);
if (p->val != q->val) return false;
if (!helper(p->left, q->right)) return false;
return helper(p->right, q->left);
}

#### 方法二：迭代法

bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
stack<TreeNode*> s1, s2;
s1.push(root->left), s2.push(root->right);
while (!s1.empty() && !s2.empty()) {
TreeNode *temp1 = s1.top();
TreeNode *temp2 = s2.top();
s1.pop();//pop返回值为void，必须先top再pop
s2.pop();
if ((!temp1 && temp2) || (temp1 && !temp2)) return false;
if (temp1 && temp2) {
if (temp1->val != temp2->val) return false;
s1.push(temp1->left);
s1.push(temp1->right);
s2.push(temp2->right);
s2.push(temp2->left);
}
}
return true;
}

#### 【LeetCode】101. Symmetric Tree 解题报告

2016-05-30 23:06:35

#### LeetCode(101)Symmetric Tree

2014-01-17 06:48:54

#### 101. Symmetric Tree [easy] (Python)

2016-06-03 15:53:34

#### 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree（对称树）】

2015-08-07 07:29:25

#### 【leetcode c++】101 Symmetric Tree

2015-07-27 14:39:02

#### [leetcode] 101. Symmetric Tree 解题报告

2015-12-24 15:30:34

#### leetcode 101. Symmetric Tree（C语言，二叉树，递归，判对称性）30

2017-10-12 13:28:11

#### 101. Symmetric Tree Leetcode Python

2015-01-30 07:28:30

#### Leetcode 101. Symmetric Tree 验证树的对称性 解题报告

2016-09-04 15:48:23

#### 【LeetCode】Symmetric Tree 解题报告

2016-05-08 15:01:08

## 不良信息举报

101. Symmetric Tree