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

//判断是否是递归树
/**
 * 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 *root) {
        if(!root) return true;
        if(root->left==NULL && root->right==NULL)
            return true;
        
        bool res = isHelp(root->left,root->right);
        return res;
        
    }
    
    bool isHelp(TreeNode *left,TreeNode* right){
        if (!left && !right) return true; 
        if (!left || !right) return false; 
            return (left->val == right->val) && isHelp(left->left, right->right)&& isHelp(left->right, right->left);
    }
};

 

posted on 2017-03-08 12:23  123_123  阅读(104)  评论(0编辑  收藏  举报