查看: 2331| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

Leetcode 366 有没有好的BFS算法?

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
小伙伴们,leetcode 366有没有BFS的解法呀?在网上找到的都是dfs解法。我想,只要是tree相关的题目,应该都是有BFS和DFS两种解法的。知道BFS解法的,麻烦告诉一声呀~多谢多谢!( P.S., 半个多月不刷,回头再看题目,竟然有些生疏了,
附上题目描述:Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.

Example:
Given binary tree
          1
         / \
        2   3
       / \     
      4   5   
Returns [4, 5, 3], [2], [1].

上一篇:LC 287 Find the Duplicate Number 二分查找的方法看不懂
下一篇:求改错:leetcode balanced binary tree
🔗
muybienw 2016-8-16 09:18:10 | 只看该作者
全局:
可以记录每个node的out degree,然后把所有的leaves放到第一层。每次处理好一个leaf,就减去parent node的out degree。degree减为0的放进下一层。不过这样需要额外记录parent和child的关系。

其实把这题想成undirected graph,有点像leetcode 310,minimum height trees
回复

使用道具 举报

🔗
forteller 2016-8-16 16:00:43 | 只看该作者
全局:
Topological sorting
回复

使用道具 举报

🔗
annawuyi 2016-8-16 18:45:16 | 只看该作者
全局:
请问楼主leetcode 现在有多少题了?谢谢
回复

使用道具 举报

🔗
 楼主| littlebearull 2016-8-16 23:10:10 | 只看该作者
全局:
muybienw 发表于 2016-8-16 09:18
可以记录每个node的out degree,然后把所有的leaves放到第一层。每次处理好一个leaf,就减去parent node的o ...

多谢您的回复!利用节点的out-degree,确实可以。不过这样的话,相当于遍历2遍,对吧。以下是我写的代码,主要是用两个hashmap分别存每个节点的children,以及它的parent。麻烦您帮忙看一下,还有可以优化的地方吗?这个bfs的运行时间是dfs的2倍呢,:( 非常感谢!
public List<List<Integer>> findLeaves(TreeNode root) {
        List<List<Integer>> res=new ArrayList<>();
        if(root==null) return res;
        HashMap<TreeNode,List<TreeNode>> children=new HashMap<>();
        HashMap<TreeNode,TreeNode> parent=new HashMap<>();
        Queue<TreeNode> queue=new LinkedList<>();queue.offer(root);
        //get the parent-children relation
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(!children.containsKey(cur)) children.put(cur,new ArrayList<>());
            if(cur.left!=null){
                children.get(cur).add(cur.left);parent.put(cur.left,cur);queue.offer(cur.left);
            }
            if(cur.right!=null){
                children.get(cur).add(cur.right);parent.put(cur.right,cur);queue.offer(cur.right);
            }
        }
        //topological sort
        for(TreeNode cur:children.keySet()){
            if(children.get(cur).isEmpty()) {queue.offer(cur);}
        }
        while(!queue.isEmpty()){
            int size=queue.size();List<Integer> tmp=new ArrayList<>();
            for(int i=0;i<size;i++){
                TreeNode cur=queue.poll();tmp.add(cur.val);
                TreeNode pa=parent.get(cur);
                    if(pa!=null) {
                        children.get(parent.get(cur)).remove(cur);
                               if(children.get(pa).isEmpty())
                                    queue.offer(pa);
                }
            }
            res.add(tmp);
        }
        return res;
    }
回复

使用道具 举报

🔗
 楼主| littlebearull 2016-8-16 23:11:03 | 只看该作者
全局:

谢谢回复!我已经按照topological sorting写了以下代码,如果您发下我的代码有可以优化的地方,麻烦告知以下哦~多谢多谢!共同进步!:)
回复

使用道具 举报

🔗
 楼主| littlebearull 2016-8-16 23:11:59 | 只看该作者
全局:
annawuyi 发表于 2016-8-16 18:45
请问楼主leetcode 现在有多少题了?谢谢

今天看了以下,已经有385道题了~~ 这出新题的速度太快了,:(
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表