LeetCode 101
Symmetric Tree
Problem Description:
判断给出的二叉树是否是对称二叉树。
具体的题目信息:
https://leetcode.com/problems/symmetric-tree/description/- Example:
- Solution:
/**
* 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 Symmetric(root->left, root->right);
}
bool Symmetric(TreeNode* l, TreeNode* r) {
if (l == NULL && r == NULL) return true;
if (l == NULL || r == NULL) return false;
if (l->val == r->val) return Symmetric(l->left, r->right) &&Symmetric(l->right, r->left);
return false;
}
};