-3
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;
我希望收到true,但我收到false。我想我已經錯過了一兩點。我應該如何解決它?我的樹遞歸方法無法正常工作
我認爲它應該是'返回isMirror(left.left,right.left)&& isMirror(left.right,right.right);'。 – Tunaki
開始使用調試器的好時機。 –
爲什麼調試時你可以直接來到SO並且有人爲你調試........? – redFIVE