首页 > 其他 > 详细

Symmetric Tree

时间:2015-10-15 18:26:32      阅读:274      评论:0      收藏:0      [点我收藏+]

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 a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool isTwoSym(struct TreeNode *leftRoot, struct TreeNode *rightRoot)
{
    int leftR, rightR;
    if(leftRoot == NULL && rightRoot == NULL)
        return true;
    else if(leftRoot == NULL && rightRoot != NULL)
        return false;
    else if(leftRoot != NULL && rightRoot == NULL)
        return false;
    else if(leftRoot->val == rightRoot->val)
        {
            leftR = isTwoSym(leftRoot->left, rightRoot->right);
            rightR = isTwoSym(leftRoot->right, rightRoot->left);
            return(leftR && rightR);
        }
    else
        return false;
}

bool isSymmetric(struct TreeNode* root) {
    bool left, right;
    if(root == NULL)
        return true;
    else 
        return isTwoSym(root, root);
}

Symmetric Tree

原文:http://www.cnblogs.com/dylqt/p/4882696.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!