我的树递归方法无法正常工作

时间:2016-03-08 19:03:25

标签: java recursion

 public static boolean isMirror(TreeNode left, TreeNode right){
        if (left==null && right==null){
            return true;
        }
        if (left!=null && right!=null) {
            if (left.data == right.data) {
                return (isMirror(left.left, left.right) && isMirror(right.left, right.right));
            }

        }
        return false;

    }

    public static boolean isSymmetric(TreeNode root){
        if (root==null){
            return true;
        }
        return isMirror(root.left, root.right);
    }


    public static void main(String[] args){
        TreeNode root=new TreeNode();
        TreeNode n1=new TreeNode();
        TreeNode n2=new TreeNode();
        TreeNode n3=new TreeNode();
        TreeNode n4=new TreeNode();

        root.left=n1;
        root.right=n2;
        n1.left=n3;
        n2.right=n4;


        root.data=3;
        n1.data=6;
        n2.data=6;
        n3.data=1;
        n4.data=1;

我希望收到真实但我收到错误。我想我错过了一两点。我该如何解决?

0 个答案:

没有答案