【leetcode】P101对称二叉树

递归,求一个二叉树是否对称,即是求左右子树是否对称,由此可以构建一个递归过程,以左右节点为参数,判断是否堆成,要求左右节点值相等并且左右节点的孩子节点互为镜像对称节点

//给定一个二叉树,检查它是否是镜像对称的。 
//
// 
//
// 例如,二叉树 [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;
        }
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值