public class TestD {
// 1
// 2 2
// 3 4 4 3
public static void main(String[] args) {
}
public static boolean judgeTreeIsMirror(TreeNode head) {
if (head == null) {
return true;
}
return isMirror(head.l, head.r);
}
public static boolean isMirror(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
if (left.val != right.val) {
return false;
}
return isMirror(left.r, right.l) && isMirror(left.l, right.r);
}
class TreeNode {
int val;
TreeNode l;
TreeNode r;
public TreeNode(int val, TreeNode l, TreeNode r) {
this.val = val;
this.l = l;
this.r = r;
}
}
}
【算法题】判断树是否是镜像的
最新推荐文章于 2023-07-06 23:31:21 发布