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

Leetcode:Symmetric Tree 判断对称树

时间:2014-06-15 18:55:36      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   tar   

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

 

解题分析:

二叉树递归,始终是第一颗二叉树的左子树和第二颗二叉树的右子树相比较

class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        if (root == nullptr) return true;
        return SymmeFunc(root->left, root->right);
    }
    bool SymmeFunc(TreeNode* left, TreeNode* right) {
        if (left == nullptr && right == nullptr) {
            return true;
        } else if (left != nullptr && right != nullptr) {
            return ((left->val == right->val) && SymmeFunc(left->left, right->right) && SymmeFunc(left->right, right->left));
        } else {
            return false;
        }
    }
};

 

Leetcode:Symmetric Tree 判断对称树,布布扣,bubuko.com

Leetcode:Symmetric Tree 判断对称树

标签:style   class   blog   code   http   tar   

原文地址:http://www.cnblogs.com/wwwjieo0/p/3785843.html

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