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
Note:
Bonus points if you could solve it both recursively and iteratively.
1. 递归:这题关键是把内部镜像转化为外部互动镜像,这样才能递归下去,否则怎么都不能把总树自成镜像问题拆成子树自成镜像问题啊,丢失了信息。
一个树是镜像的就是它的两个子树互成镜像。两棵树互成镜像就是两棵树的根相等且树1.左和树2.右互成镜像,且树1.右和树2.左互成镜像。
2.迭代:类似于BFS(不需要层级遍历)。就是每次加入的点的顺序要按镜像来。
细节:一开始可以传入两次root来解决第一个点略不一样的问题
1.递归
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isSymmetric(TreeNode root) { return helper(root, root); } private boolean helper(TreeNode n1, TreeNode n2) { if (n1 == null && n2 == null) { return true; } if (n1 == null || n2 == null) { return false; } return n1.val == n2.val && helper(n1.left, n2.right) && helper(n1.right, n2.left); } }
2. 迭代
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isSymmetric(TreeNode root) { Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.offer(root); queue.offer(root); while (!queue.isEmpty()) { TreeNode n1 = queue.poll(); TreeNode n2 = queue.poll(); // 注意等于与否不要担心null,只要不一样就会输出false. null == null-> true, null != null -> false // null == 某个体 -> false // 记得要比value,不是比个体(地址) if (n1 == null && n2 == null) { continue; } if (n1 == null || n2 == null || n1.val != n2.val) { return false; } queue.offer(n1.left); queue.offer(n2.right); queue.offer(n1.right); queue.offer(n2.left); } return true; } }
3.自己写的很丑的迭代
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isSymmetric(TreeNode root) { Queue<TreeNode> queue = new LinkedList<TreeNode>(); if (root == null || root.left == null && root.right == null) { return true; } else if (root.left == null || root.right == null || root.left.val != root.right.val) { return false; } else { queue.offer(root.left); queue.offer(root.right); } while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i + 1 < size; i += 2) { TreeNode n1 = queue.poll(); TreeNode n2 = queue.poll(); // 注意等于与否不要担心null,只要不一样就会输出false. null == null-> true, null != null -> false // null == 某个体 -> false // 记得要比value,不是比个体(地址) if(n1.left == null && n2.right != null || n1.left != null && n2.right == null || n2.left == null && n1.right != null || n2.left != null && n1.right == null) { return false; } if (n1.left != null && n2.right != null && n1.left.val != n2.right.val) { return false; } if (n1.right != null && n2.left != null && n1.right.val != n2.left.val) { return false; } if (n1.left != null) { queue.offer(n1.left); queue.offer(n2.right); } if (n1.right != null) { queue.offer(n1.right); queue.offer(n2.left); } } } return true; } }