101、对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [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
说明:
如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
题解
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 isSymmetric(root,root);
}
private boolean isSymmetric(TreeNode root1,TreeNode root2) {
if (root1 == null && root2 == null) {
return true;
}
if (root1 == null || root2 == null) {
return false;
}
return root1.val == root2.val && isSymmetric(root1.left,root2.right) && isSymmetric(root1.right,root2.left);
}
}
2、层序遍历
层序遍历,对于左右结点进行交叉加入队列,在出队时进行比较。
- 代码
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
q.add(root);
while (!q.isEmpty()) {
TreeNode t1 = q.poll();
TreeNode t2 = q.poll();
if (t1 == null && t2 == null) continue;
// 不对称
if (t1 == null || t2 == null) return false;
// 不对称
if (t1.val != t2.val) return false;
q.add(t1.left);
q.add(t2.right);
q.add(t1.right);
q.add(t2.left);
}
return true;
}
> 链接:https://leetcode-cn.com/problems/symmetric-tree/solution/dui-cheng-er-cha-shu-by-leetcode/
递归、bfs-层序遍历、DFS-先、中、后序遍历
/** 递归 和官方题解的一样 可以跳过 */
class Solution {
private class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int v) {
val = v;
}
}
public boolean isSymmetric(TreeNode root) {
return isSymmetric(root, root);
}
public boolean isSymmetric(TreeNode p, TreeNode q) {
if(p == null && q == null)
return true;
if(p == null || q == null)
return false;
return (p.val == q.val) && isSymmetric(p.left, q.right) && isSymmetric(p.right, q.left);
}
}
/** bfs 我选择只用一个队列 用两个队列我也试了一下 效率差不多 )*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
return help(root.left, root.right);
}
private boolean help(TreeNode p, TreeNode q){
if (p == null && q == null)
return true;
if (p == null || q == null)
return false;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(p);
queue.add(q);
while (!queue.isEmpty()) {
TreeNode nodeP = queue.poll();
TreeNode nodeQ = queue.poll();
if (nodeP == null && nodeQ == null)
continue;
if (nodeP == null || nodeQ == null)
return false;
if (nodeP.val != nodeQ.val)
return false;
else {
queue.add(nodeP.left);
queue.add(nodeQ.right);
queue.add(nodeP.right);
queue.add(nodeQ.left);
}
}
return true;
}
}
/** dfs 前序遍历 */
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
return help(root.left, root.right);
}
private boolean help(TreeNode p, TreeNode q) {
if (p == null && q == null)
return true;
if (p == null || q == null)
return false;
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
stack1.push(p);
stack2.push(q);
while (!stack1.isEmpty() && !stack2.isEmpty()) {
TreeNode nodeP = stack1.pop();
TreeNode nodeQ = stack2.pop();
if (nodeP == null && nodeQ == null)
continue;
if (nodeP == null || nodeQ == null)
return false;
if (nodeP.val != nodeQ.val)
return false;
else {
stack1.push(nodeP.left);
stack1.push(nodeP.right);
stack2.push(nodeQ.right);
stack2.push(nodeQ.left);
}
}
return true;
}
}
/** dfs 中序遍历 */
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
return help(root.left, root.right);
}
private boolean help(TreeNode p, TreeNode q) {
if (p == null && q == null)
return true;
if (p == null || q == null)
return false;
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
while (p != null || q != null || !stack1.isEmpty() || !stack2.isEmpty()) {
if (p != null && q != null) {
stack1.push(p);
stack2.push(q);
p = p.left;
q = q.right;
}
else if (p == null && q == null) {
p = stack1.pop();
q = stack2.pop();
if (p.val != q.val)
return false;
p = p.right;
q = q.left;
}
else
return false;
}
return true;
}
}
/** dfs后序遍历 感觉这个遍历方法还是有点啰嗦 要是大家有更好的方法可以在评论里留言哈 谢谢~ */
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
return help(root.left, root.right);
}
private boolean help(TreeNode p, TreeNode q) {
if (p == null && q == null)
return true;
if (p == null || q == null)
return false;
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack1Temp = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
Stack<TreeNode> stack2Temp = new Stack<>();
while (p != null || q != null || !stack1Temp.isEmpty() || !stack2Temp.isEmpty()) {
if (p != null && q != null) {
stack1.push(p);
stack1Temp.push(p);
stack2.push(q);
stack2Temp.push(q);
p = p.left;
q = q.right;
}
else if (p == null && q == null) {
p = stack1Temp.pop();
q = stack2Temp.pop();
if (p.val != q.val)
return false;
p = p.right;
q = q.left;
}
else
return false;
}
while (!stack1.isEmpty() || !stack2.isEmpty()) {
if (stack1.pop().val != stack2.pop().val)
return false;
}
return true;
}
}
// 作者:bu-zhan-bian-yi-jiu-yao-chi-yu
// 链接:https://leetcode-cn.com/problems/symmetric-tree/solution/zheng-li-liao-yi-xia-er-cha-shu-de-ji-chong-jie-fa/
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com