美文网首页
2019-04-09 BFS广度优先搜索刷题Day1

2019-04-09 BFS广度优先搜索刷题Day1

作者: 放开那只三级头 | 来源:发表于2019-04-09 17:10 被阅读0次

Leetcode 101 对称二叉树

题目描述:

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

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

    1
  2   2
3  4 4  3

下面这个就是不对称的

    1
   / \
  2   2
   \   \
   3    3

思路:

类似层次遍历的方法,维护一个队列,注意根节点的左孩子和右孩子先按顺序进去,之后的层的节点数目已经大于4,所以可以按照一定顺序加入队列:左左,右右,左右,右左,之后循环比较每次弹出的两个节点值是否相等即可。

代码:

/**
 * 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;
        }
        if (root.left==null && root.right==null){
            return true;
        }
        if (root.left!=null&&root.right==null || root.left==null&&root.right!=null){
            return false;
        }
        
        LinkedList<TreeNode> queue=new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);
        
        while (!queue.isEmpty()){
            TreeNode le=queue.poll();
            TreeNode ri=queue.poll();
            if (le==null&&ri==null){
                continue;
            }
            if (le==null||ri==null||le.val!=ri.val){
                return false;
            }
            queue.offer(le.left);
            queue.offer(ri.right);
            queue.offer(le.right);
            queue.offer(ri.left);
        }
        return true;
    }
}

LeetCode 127 单词接龙

题目描述

给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:

  1. 每次转换只能改变一个字母。
  2. 转换过程中的中间单词必须是字典中的单词。

说明:

  • 如果不存在这样的转换序列,返回 0。
  • 所有单词具有相同的长度。
  • 所有单词只由小写字母组成。
  • 字典中不存在重复的单词。
  • 你可以假设 beginWord 和 endWord 是非空的,且二者不相同。

示例1

输入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
输出: 5
解释: 一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog",
返回它的长度 5。

代码

class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        if (wordList==null || !wordList.contains(endWord)){
            return 0;
        }
        Queue<String> queue=new LinkedList<String>();
        Set<String> words=new HashSet<>(wordList);
        queue.offer(beginWord);
        int count=1;
        while (!queue.isEmpty()){
            int size=queue.size();
            count++;
            for (int i=0;i<size;i++){
                String word=queue.poll();
                List<String> candidates=bfs(words,word);
                for (String candidate:candidates){
                    if (endWord.equals(candidate)){
                        return count;
                    }
                    queue.offer(candidate);
                }
            }
        }
        return 0;
    }
    
    public List<String> bfs(Set<String> words,String word){
        List<String> candidates=new ArrayList<>();
        StringBuffer sb=new StringBuffer(word);
        for (int i=0;i<sb.length();i++){
            char temp=sb.charAt(i);
            for (char c='a';c<='z';c++){
                if (temp==c){
                    continue;
                }
                sb.setCharAt(i,c);
                String newWord=new String(sb);
                if (words.remove(newWord)){
                    candidates.add(newWord);
                }
            }
            sb.setCharAt(i,temp);
        }
        return candidates;
    }
}

相关文章

网友评论

      本文标题:2019-04-09 BFS广度优先搜索刷题Day1

      本文链接:https://www.haomeiwen.com/subject/agdiiqtx.html