图论16(Leetcode863.二叉树中所有距离为K的结点)

答案:

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
        Map<Integer, int[]> map = new HashMap<>();
        int[] link = {-1,-1,-1};
        map.put(root.val,link);
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(queue.size()!=0){
            TreeNode cur = queue.poll();
            int[] link1 = new int[3];
            int[]temp = map.get(cur.val);//cur包含父节点的link
            link1[0] = temp[0];
            link1[1] = -1;
            link1[2] = -1;
            int[] link2 = {cur.val,-1,-1};//记录left right的父节点
            if(cur.left!=null){
                TreeNode left = cur.left;
                link1[1] = left.val;
                map.put(left.val,link2);
                queue.add(left);

            }
            if(cur.right!=null){
                TreeNode right = cur.right;
                link1[2] = right.val;
                map.put(right.val,link2);
                queue.add(right);
            }
            map.put(cur.val,link1);
        }

        map.forEach((key, value) -> System.out.println("Key = " + key + ", Value = " + value[0] +" "+ value[1]+" "+ value[2]));


        List<Integer> res = new ArrayList<>();
        Queue<Integer> queue2 = new LinkedList<>();
        Set<Integer> set = new HashSet<>();
        queue2.add(target.val);
        set.add(target.val);
        int step = 0;
        while(queue2.size()!=0){
            if(step==k){
                while(queue2.size()!=0){
                    res.add(queue2.poll());
                }
                break;
            }
            int len = queue2.size();
            for(int i=0;i<len;i++){
                int node = queue2.poll();
                int value[] = map.get(node);
                if(value[0]!=-1&&!set.contains(value[0])){
                    queue2.add(value[0]);
                    set.add(value[0]);
                }
                if(value[1]!=-1&&!set.contains(value[1])){
                    queue2.add(value[1]);
                    set.add(value[1]);
                }
                if(value[2]!=-1&&!set.contains(value[2])){
                    queue2.add(value[2]);
                    set.add(value[2]);
                }

            }
            step++;
        }
        return res;
    }
}
相关推荐
计信金边罗3 天前
是否存在路径(FIFOBB算法)
算法·蓝桥杯·图论
闻缺陷则喜何志丹5 天前
【二分图 图论】P9384 [THUPC 2023 决赛] 着色|普及+
c++·算法·图论·二分图·洛谷
鸽子炖汤5 天前
LRC and VIP
c++·算法·图论
JK0x077 天前
代码随想录算法训练营 Day61 图论ⅩⅠ Floyd A※ 最短路径算法
算法·图论
qq_447429417 天前
数据结构与算法:图论——拓扑排序
linux·c语言·学习·图论
zc.ovo7 天前
图论刷题1
算法·深度优先·图论
珂朵莉MM7 天前
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)解题报告 | 珂学家
人工智能·算法·职场和发展·深度优先·图论
蒙奇D索大7 天前
【数据结构】图论核心算法解析:深度优先搜索(DFS)的纵深遍历与生成树实战指南
数据结构·算法·深度优先·图论·图搜索算法
ShiinaMashirol7 天前
代码随想录打卡|Day50 图论(拓扑排序精讲 、dijkstra(朴素版)精讲 )
java·图论
JK0x0710 天前
代码随想录算法训练营 Day60 图论Ⅹ Bellmen_ford 系列算法
android·算法·图论