力扣搜索

BFS 广度优先搜索

广度优先搜索:广度优先搜索一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
在程序实现 BFS 时需要考虑以下问题:

队列:用来存储每一轮遍历得到的节点;
标记:对于遍历过的节点,应该将它标记,防止重复遍历。

101. 对称二叉树

1.要求

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [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

说明:

如果你可以运用递归和迭代两种方法解决这个问题,会很加分

2.思路

1.递归查找对比是否对称;
2.利用BFS 遍历比较队列中左右两边的元素是否对称;

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return isMirror(root,root);
        
    }
    public static boolean isMirror(TreeNode left,TreeNode right){
  
        if(left==null&& right==null) return true;
        if(left==null || right==null) return false;
        return (left.val==right.val) && isMirror(left.left,right.right)&& isMirror(left.right,right.left);
        }
        }
        

class Solution {
    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;
        
    }
}

279 完全平方数

1 要求

给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。

示例 1:

输入: n = 12
输出: 3
解释: 12 = 4 + 4 + 4.
示例 2:

输入: n = 13
输出: 2
解释: 13 = 4 + 9.

2 思路

将问题转化成图论
该算法在往队列里面添加节点的时候会 add 很多重复的节点,导致超时,
优化办法是,加入 visited 数组,检查要 add 的数据是否已经出现过了,防止数据重复出现,从而影响图的遍历;

class Solution {
    public int numSquares(int n) {
        List<Integer>  squares=genrateSqute(n);
        Queue<Integer> queue = new LinkedList<>();
        boolean[] visted = new boolean[n + 1];
        queue.add(n);
        visted[n]=true;
        int level=0;
        while(!queue.isEmpty()){
            int size=queue.size();
            level++;
            while(size-->0){
                int cur=queue.poll();
                for(int s : squares){
                    int next=cur-s;
                    if(next<0){
                        break;
                    }
                    if(next==0){
                        return level;
                    }
                    if(visted[next]){
                        continue;
                    }
                    visted[next]=true;
                    queue.add(next);
                }
            }
            
            
        }
        
        
        
        
        return 1;
        
    }
    public static  List<Integer> genrateSqute(int n){
        List<Integer> squares=new ArrayList<>();
        int square=1;
        int i=1;
        while(i<=n){
            squares.add(square);
            square=(i+1)*(i+1);
            i++;
        }
        return squares;
        
    }
}

DFS 深度优先搜索

待续…

backtracking 回溯

待续…

链接:https://github.com/CyC2018/CS-Notes/blob/master/notes/Leetcode%20%E9%A2%98%E8%A7%A3%20-%20%E6%90%9C%E7%B4%A2.md
https://leetcode-cn.com/problems/symmetric-tree

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值