代码改变世界

leetcode - Symmetric Tree

2013-10-20 09:58  张汉生  阅读(162)  评论(0编辑  收藏  举报

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode * left, TreeNode * right){
        if (left==NULL && right==NULL)
            return true;
        if (left==NULL || right==NULL)
            return false;
        if (left->val!=right->val)
            return false;
        return isSymmetric(left->left,right->right) && isSymmetric(left->right,right->left);
    }
    bool isSymmetric(TreeNode *root) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if (root==NULL)
            return true;
        return isSymmetric(root->left,root->right);
    }
};