递归,求一个二叉树是否对称,即是求左右子树是否对称,由此可以构建一个递归过程,以左右节点为参数,判断是否堆成,要求左右节点值相等并且左右节点的孩子节点互为镜像对称节点
//给定一个二叉树,检查它是否是镜像对称的。
//
//
//
// 例如,二叉树 [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
//
//
//
//
// 进阶:
//
// 你可以运用递归和迭代两种方法解决这个问题吗?
// Related Topics 树 深度优先搜索 广度优先搜索
// 👍 1102 👎 0
package leetcode.editor.cn;
//Java:对称二叉树
public class P101SymmetricTree {
public static void main(String[] args) {
P101SymmetricTree p101 = new P101SymmetricTree();
Solution solution = p101.new Solution();
TreeNode p = p101.new TreeNode(1);
p.left = p101.new TreeNode(2);
p.right = p101.new TreeNode(2);
p.left.left = p101.new TreeNode(3);
p.left.right = p101.new TreeNode(4);
p.right.left = p101.new TreeNode(4);
p.right.right = p101.new TreeNode(3);
boolean symmetric = solution.isSymmetric(p);
System.out.println(symmetric);
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* 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;
else return isSymmetric(root.left, root.right);
}
public boolean isSymmetric(TreeNode a, TreeNode b) {
if (a == null && b == null)
return true;
else if (a == null || b == null)
return false;
else if (a.val != b.val)
return false;
else if (!isSymmetric(a.left, b.right) || !isSymmetric(a.right, b.left))
return false;
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}
迭代,基于队列和层次遍历的思路将递归改成迭代,每次从队列取出两个节点进行判断,每次入队时如一对镜像位置的节点,比如将左树左孩子和右数右孩子,左树右孩子和右树左孩子,只要root节点不为空,就将左右孩子节点入队,判断值是否相等,不等直接结束,返回false,相等则将两棵树的各自孩子镜像入队,队列为空结束
//给定一个二叉树,检查它是否是镜像对称的。
//
//
//
// 例如,二叉树 [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
//
//
//
//
// 进阶:
//
// 你可以运用递归和迭代两种方法解决这个问题吗?
// Related Topics 树 深度优先搜索 广度优先搜索
// 👍 1102 👎 0
package leetcode.editor.cn;
import java.util.LinkedList;
//Java:对称二叉树
public class P101SymmetricTree {
public static void main(String[] args) {
P101SymmetricTree p101 = new P101SymmetricTree();
Solution solution = p101.new Solution();
TreeNode p = p101.new TreeNode(1);
p.left = p101.new TreeNode(2);
p.right = p101.new TreeNode(2);
p.left.left = p101.new TreeNode(3);
p.left.right = p101.new TreeNode(4);
p.right.left = p101.new TreeNode(4);
p.right.right = p101.new TreeNode(3);
boolean symmetric = solution.isSymmetric(p);
System.out.println(symmetric);
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* 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 isSymmetric(root.left, root.right);
}
public boolean isSymmetric(TreeNode a, TreeNode b) {
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(a);
queue.offer(b);
while (!queue.isEmpty()) {
a = queue.poll();
b = queue.poll();
if (a == null && b == null)
continue;
if (a == null || b == null)
return false;
if (a.val != b.val)
return false;
queue.offer(a.left);
queue.offer(b.right);
queue.offer(a.right);
queue.offer(b.left);
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}