码迷,mamicode.com
首页 > 其他好文 > 详细

Symmetric Tree

时间:2015-03-19 23:46:10      阅读:152      评论:0      收藏: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 isTrue(TreeNode *root1,TreeNode *root2){
        if(!root1 && !root2) return true;
        else if(!root1||!root2) return false;
        return root1->val == root2->val && isTrue(root1->left,root2->right)&&isTrue(root1->right,root2->left);
    }
    bool isSymmetric(TreeNode *root) {
        if(!root) return true;
        return isTrue(root->left,root->right);
    }
};

 

Symmetric Tree

标签:

原文地址:http://www.cnblogs.com/llei1573/p/4352196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!