《剑指offer—面试题28:对称的二叉树》

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011296723/article/details/81739120

《剑指offer—面试题28:对称的二叉树》
注明:仅个人学习笔记

先序遍历的序列 等于 对称先序遍历的序列
当树中所有节点均相同时,遍历时,序列中带入null,便可区分

package com.chapter3.code;

public class IsSymmetrical28
{
boolean isSymmetrical(TreeNode root)
{

    return isSymmetrical(root, root);
}

private boolean isSymmetrical(TreeNode root1, TreeNode root2)
{

    if (root1 == null && root2 == null)
    {
        return true;
    }


    //此时,只有一个为空,就说明不对称了
    if (root1 == null || root2 == null)
    {
        return false;
    }

    if (root1.value != root2.value)
    {
        return false;
    }

    return isSymmetrical(root1.left, root2.right) && isSymmetrical(root1.right, root2.left);

}

}

猜你喜欢

转载自blog.csdn.net/u011296723/article/details/81739120