图论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;
    }
}
相关推荐
周哈里窗的编程16 小时前
CSP-CCF★201912-2回收站选址★
c++·算法·图论
Aurora_th1 天前
树与图的深度优先遍历(dfs的图论中的应用)
c++·算法·深度优先·图论·dfs·树的直径
夏天天天天天天天#1 天前
求Huffman树及其matlab程序详解
算法·matlab·图论
浅念同学1 天前
算法.图论-建图/拓扑排序及其拓展
算法·图论
Greyplayground1 天前
【算法基础实验】图论-BellmanFord最短路径
算法·图论·最短路径
逝去的秋风2 天前
【代码随想录训练营第42期 Day61打卡 - 图论Part11 - Floyd 算法与A * 算法
算法·图论·floyd 算法·a -star算法
浅念同学2 天前
算法.图论-并查集上
java·算法·图论
蠢蠢的打码2 天前
8584 循环队列的基本操作
数据结构·c++·算法·链表·图论
逝去的秋风4 天前
【代码随想录训练营第42期 Day57打卡 - 图论Part7 - Prim算法与Kruskal算法
算法·图论·prim算法
热爱编程的OP5 天前
图论篇--代码随想录算法训练营第六十一天打卡| Floyd 算法,A*算法
数据结构·c++·学习·算法·图论