如题
一眼看到题目,感觉可以镜像遍历,然后比较结果
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
List<Integer> l1 = new ArrayList<Integer>();
List<Integer> l2 = new ArrayList<Integer>();
leftTraversal(l1, root);
rightTraversal(l2, root);
return l1.equals(l2);
}
//先遍历左子树
private void leftTraversal(List<Integer> li, TreeNode root) {
li.add(root.val);
if (root.left != null && root.right != null) {
leftTraversal(li, root.left);
leftTraversal(li, root.right);
} else if (root.left == null && root.right != null) {
li.add(null);
leftTraversal(li, root.right);
} else if (root.left != null && root.right == null) {
leftTraversal(li, root.left);
li.add(null);
}
}
//先遍历右子树
private void rightTraversal(List<Integer> li, TreeNode root) {
li.add(root.val);
if (root.left != null && root.right != null) {
rightTraversal(li, root.right);
rightTraversal(li, root.left);
} else if (root.left == null && root.right != null) {
rightTraversal(li, root.right);
li.add(null);
} else if (root.left != null && root.right == null) {
li.add(null);
rightTraversal(li, root.left);
}
}
结果
很显然又优化余地,左右镜像只用比较左右子树即可
public boolean isSymmetric1(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) {
return true;
} else if (root.left == null || root.right == null) {
return false;
}
List<Integer> l1 = new ArrayList<Integer>();
List<Integer> l2 = new ArrayList<Integer>();
leftTraversal(l1, root.left);
rightTraversal(l2, root.right);
return l1.equals(l2);
}
额,效率上并没有什么进步。那就换个思路,直接递归
public boolean isSymmetric2(TreeNode root) {
return root == null ? true : res(root.left, root.right);
}
private boolean res(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null || left.val != right.val) {
return false;
}
return res(left.left, right.right) && res(left.right, right.left);
}
效率大大提升,但是总感觉还是有优化余地,是否可以匹配到不等直接退出