力扣labuladong——一刷day54

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣1660. 纠正二叉树
  • 二、力扣100. 相同的树
  • 三、力扣572. 另一棵树的子树
  • 四、力扣101. 对称二叉树


前言


判断树是否相等,就直接遍历就可以。判断一颗树是否是另外一棵树的子树,遍历当前树,遍历每一个节点的时候,调用判断树是否相同的方法。判断是否是对称二叉树,原树的左子树中的每一个孩子,与原树的右子树的每一个孩子进行比较,比较规则是,原树的左子树中的每一个孩子的左孩子与原树的右子树的每一个孩子的右孩子比较,原树的左子树中的每一个孩子的右孩子与原树的右子树的每一个孩子的左孩子比较

一、力扣1660. 纠正二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    HashSet<TreeNode> set = new HashSet<>();
    public TreeNode correctBinaryTree(TreeNode root) {
        if(root == null){
            return null;
        }
        if(set.contains(root.right)){
            return null;
        }
        set.add(root);
        root.right = correctBinaryTree(root.right);
        root.left = correctBinaryTree(root.left);
        return root;
    }
}

二、力扣100. 相同的树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        if(p.val != q.val){
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }
}

三、力扣572. 另一棵树的子树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null){
            return subRoot == null;
        }
        if(fun(root,subRoot)){
            return true;
        }
        return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);
    }
    public boolean fun(TreeNode p, TreeNode q){
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        if(p.val != q.val){
            return false;
        }
        return fun(p.left,q.left) && fun(p.right , q.right);
    }
}

四、力扣101. 对称二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        return fun(root.left,root.right);
    }
    public boolean fun(TreeNode l, TreeNode r){
        if(l == null && r == null){
            return true;
        }
        if(l == null || r == null){
            return false;
        }
        if(l.val != r.val){
            return false;
        }
        return fun(l.left , r.right) && fun(l.right , r.left);
    }
}

你可能感兴趣的:(力扣题解,leetcode,算法,数据结构,java)