You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
644 B
31 lines
644 B
2 years ago
|
package class30;
|
||
|
|
||
|
public class Problem_0101_SymmetricTree {
|
||
|
|
||
|
public static class TreeNode {
|
||
|
int val;
|
||
|
TreeNode left;
|
||
|
TreeNode right;
|
||
|
}
|
||
|
|
||
|
public boolean isSymmetric(TreeNode root) {
|
||
|
return isMirror(root, root);
|
||
|
}
|
||
|
|
||
|
// 一棵树是原始树 head1
|
||
|
// 另一棵是翻面树 head2
|
||
|
public static boolean isMirror(TreeNode head1, TreeNode head2) {
|
||
|
if (head1 == null && head2 == null) {
|
||
|
return true;
|
||
|
}
|
||
|
if (head1 != null && head2 != null) {
|
||
|
return head1.val == head2.val
|
||
|
&& isMirror(head1.left, head2.right)
|
||
|
&& isMirror(head1.right, head2.left);
|
||
|
}
|
||
|
// 一个为空,一个不为空 false
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
}
|