题目:
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
.1
./. \
2 . 2
/ \ ./ \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ . \
3 . 3
说明:
如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/symmetric-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:
检查其是否为镜像树,其中一棵树按照前序中序或后序进行遍历,然后另一棵树按照左右节点相反的访问顺序(左右相反,当前节点访问顺序不变)进行访问并比较他们的节点是否相同即可;注意前序遍历的相反的顺序并不是后续;
前序遍历:中左右,相反的顺序:中右左;
中序遍历:左中右,相反的顺序:右中左;
后续遍历:左右中,相反的顺序:右左中;
代码分析:
- p1取前序遍历进行访问;
- 则p2访问顺序为:中右左,满足p1.val =p2.val ,p1.left = p2.right ,p1.right = p2.left(这里的=号的含义为值相等或者都为null);
代码实现:
递归版本:Leecode时间为1ms(注意时间对比)
/**
* 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) {
if(root == null) return true;
return DFS(root.left,root.right);
}
public boolean DFS(TreeNode node1,TreeNode node2){
if(node1 == null && node2 == null) return true;
if(node1 == null || node2 == null) return false;
if(node1.val != node2.val) return false;
return DFS(node1.left,node2.right) && DFS(node1.right,node2.left);
}
}
迭代版本:Leecode 时间为3ms,(因为多次入栈出栈的原因,效率比递归版本要差,虽然此处的出入栈次数还有较大的优化空间,但个人觉得即使优化过后依然无法与迭代版本媲美,毕竟LinkedList和线程的栈帧比起来,效率还是差不少,后面的代码优化了出入栈次数,时间为2ms)
有朋友可能会问为什么不使用Stack,其是动态数组实现的,效率会更高?其实Stack有两个问题:
- Stack正是因为是动态数组实现的存在扩容问题,当扩容发生时,push效率就会受到较大影响(如果存在缩容,那么pop也会收到影响,没有阅读源码,不确定会不会缩容),而且仔细想想,Stack并不存在随机访问,动态数组的实现真的对比List有优势吗?
- Stack通过继承Vector来实现的,Vector本身是线程安全的容器,存在一些不必要的开销,另一个更大的问题是,通过继承,Stack也得到了一些它本不应该有的函数,如add(int index, E element) ,使用不当就会破坏Stack的特性;
/**
* 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) {
if(root == null) return true;
return DFS(root.left,root.right);
}
public boolean DFS(TreeNode node1,TreeNode node2){
if(node1 == null && node2 == null) return true;
if(node1 == null || node2 == null) return false;
LinkedList<TreeNode> stack1 = new LinkedList<>();
LinkedList<TreeNode> stack2 = new LinkedList<>();
stack1.push(node1);
stack2.push(node2);
while(!stack1.isEmpty() || !stack2.isEmpty()){
node1 = stack1.pop();
node2 = stack2.pop();
if(node1.val != node2.val){
return false;
}
if(node1.right != null && node2.left != null){ //都不为空,则放入stack中,下次循环再取出来比较
stack1.push(node1.right);
stack2.push(node2.left);
} else if(node1.right == null && node2.left == null){ //都为null,可认为节点对称,无需入栈
} else { //其中一个为null一个不为null,不对称
return false;
}
if(node1.left != null && node2.right != null){
stack1.push(node1.left);
stack2.push(node2.right);
} else if (node1.left == null && node2.right == null) { //都为null,无需入栈
} else { //其中一个为null一个不为null,不对称
return false;
}
}
return true;
}
}
迭代版本改为以下代码后,Leecode上时间提升到了2ms,依然无法与迭代版本媲美;
/**
* 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) {
if(root == null) return true;
return DFS(root.left,root.right);
}
public boolean DFS(TreeNode node1,TreeNode node2){
if(node1 == null && node2 == null) return true;
if(node1 == null || node2 == null) return false;
LinkedList<TreeNode> stack1 = new LinkedList<>();
LinkedList<TreeNode> stack2 = new LinkedList<>();
stack1.push(node1);
stack2.push(node2);
while(!stack1.isEmpty() || !stack2.isEmpty()){
node1 = stack1.pop();
node2 = stack2.pop();
while(node1 != null && node2 != null){
if(node1.val != node2.val){
return false;
}
if(node1.right != null && node2.left != null){//将后续需要访问的节点先存入stack
stack1.push(node1.right);
stack2.push(node2.left);
} else if(node1.right == null && node2.left == null){//后续需要访问的节点为null,无需入栈;
} else {
return false;
}
if(node1.left != null && node2.right != null){ //下一个循环需要访问的节点不为null,则不再入栈,直接赋值;
node1 = node1.left;
node2 = node2.right;
} else if (node1.left == null && node2.right == null) {//下一个循环需要访问的节点为null了,需要冲stack中拿出元素来访问;
node1 = null;
node2 = null;
} else {
return false;
}
}
}
return true;
}
}