您的位置:首页 > 编程语言 > C语言/C++

leetcode_c++:树: Symmetric Tree(101)

2016-08-24 17:31 531 查看
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


class Solution {
private:
bool isSymmetric(TreeNode *p, TreeNode *q) {
if (p == NULL)
return q == NULL;
if (q == NULL)
return p == NULL;
if (p->val != q->val)
return false;
return isSymmetric(p->left, q->right) &&
isSymmetric(p->right, q->left);
}
public:
bool isSymmetric(TreeNode *root) {
if (root == NULL)
return true;
return isSymmetric(root->left, root->right);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: