图论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;
    }
}
相关推荐
Ahu_iii1 小时前
【图论基础】理解图的“闭环”:Tarjan 强连通分量算法全解析
算法·图论
小蛋编程2 天前
【算法-图论】图的存储
c++·算法·图论
今天你睡了嘛2 天前
数据结构——图(二、图的存储和基本操作)
数据结构·算法·图论
KyollBM2 天前
【CF】Day114——杂题 (贪心 + 图论 | LCM + 贪心 | 最大最小子序列 + 图论)
算法·图论
YouQian7723 天前
绳子切割 图论
图论
KyollBM4 天前
【Luogu】每日一题——Day15. P1144 最短路计数 (记忆化搜索 + 图论 + 最短路)
算法·图论
guozhetao4 天前
【ST表、倍增】P7167 [eJOI 2020] Fountain (Day1)
java·c++·python·算法·leetcode·深度优先·图论
菜鸟555555 天前
图论:Dijkstra算法
算法·图论·dijkstra·xcpc
菜鸟555556 天前
图论:最小生成树
算法·图论
菜鸟555556 天前
图论:搜索问题
图论·搜索