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.

27 lines
571 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class06;
// 测试链接https://leetcode.com/problems/symmetric-tree
public class Code03_SymmetricTree {
public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
}
public static boolean isSymmetric(TreeNode root) {
return isMirror(root, root);
}
public static boolean isMirror(TreeNode h1, TreeNode h2) {
if (h1 == null ^ h2 == null) {
return false;
}
if (h1 == null && h2 == null) {
return true;
}
return h1.val == h2.val && isMirror(h1.left, h2.right) && isMirror(h1.right, h2.left);
}
}