对称的二叉树

public class 对称的二叉树
{
    public boolean isSymmetrical(TreeNode root)
    {
        // 特殊情况考虑
        if (root == null)
        {
            return true;
        }
        return isSymmetrical(root.left, root.right);
    }

    public boolean isSymmetrical(TreeNode root1, TreeNode root2)
    {
        // 考虑特殊情况
        if (root1 == null && root2 == null)
        {
            return true;
        }
        if (root1 == null || root2 == null)
        {
            return false;
        }
        // 左右节点同时存在 进而比较其value
        if (root1.value != root2.value)
        {
            return false;
        }
        else
        {
            // 相等即进行其子节点比较 (root1.left与root2.right)&&(root1.right与root2.left)
            return isSymmetrical(root1.left, root2.right)
                    && isSymmetrical(root1.right, root2.left);
        }
    }
}

posted @ 2018-01-06 19:14  qingtianBKY  阅读(130)  评论(0编辑  收藏  举报