Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
publicclassSymmetricTree{ publicbooleanisSymmetric(TreeNode root){ List<TreeNode> list = new ArrayList<>(); inOrderTraversal(root, list); int center = list.size() / 2; return compare(list, center); }
publicbooleancompare(List<TreeNode> list, int center){ for (int i = 0, j = list.size() - 1; i < center; i++, j--) { if (list.get(i).val != list.get(j).val) { returnfalse; } } returntrue; }