/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
vector<bool> visited(, false);
vector<unordered_set<int>> g(); dfs(root, g); vector<int> res;
queue<int> q;
q.push(target->val);
visited[target->val] = true;
int lv = ;
while (!q.empty()) {
int qs = q.size();
if (lv == K) {
while(!q.empty()) {
res.push_back(q.front());
q.pop();
}
break;
}
while (qs-- > ) {
int p = q.front();
q.pop();
for (auto i : g[p]) {
if (!visited[i]) {
q.push(i);
visited[i] = true;
}
}
}
lv++;
}
return res;
}
void dfs(TreeNode *root, vector<unordered_set<int>>& g) {
if (root == NULL) return;
if (root->left) {
g[root->val].insert(root->left->val);
g[root->left->val].insert(root->val);
}
if (root->right) {
g[root->val].insert(root->right->val);
g[root->right->val].insert(root->val);
}
dfs(root->left, g);
dfs(root->right, g);
}
};

863. All Nodes Distance K in Binary Tree的更多相关文章

  1. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  2. 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点

    [抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...

  3. leetcode 863. All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  4. [LC] 863. All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  5. [LeetCode] All Nodes Distance K in Binary Tree 二叉树距离为K的所有结点

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  6. [Swift]LeetCode863. 二叉树中所有距离为 K 的结点 | All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a targetnode, and an integer value K. Return a lis ...

  7. LeetCode – All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  8. [Leetcode] 863. All Nodes Distance K in Binary Tree_ Medium tag: BFS, Amazon

    We are given a binary tree (with root node root), a target node, and an integer value `K`. Return a ...

  9. 距离为K的节点 All Nodes Distance K in Binary Tree

    2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...

随机推荐

  1. 关于windows server 里Let's Encrypt续订的问题

    引言 Let's Encrypt是什么就不详细说了,它是免费的https证书,优点就是免费,缺点就是每三个月就要自己续上.今天主要介绍的是续上有效期的环节. 1.安装certify 下载地址: htt ...

  2. less 学习

    收藏地址 : http://www.bootcss.com/p/lesscss/

  3. RequireJS进阶-模块的优化及配置的详解

    概述 关于RequireJS已经有很多文章介绍过了.这个工具可以将你的JavaScript代码轻易的分割成苦干个模块(module)并且保持你的代码模块化与易维护性.这样,你将获得一些具有互相依赖关系 ...

  4. 阶&原根

    求阶的方法: 根据性质2,直接对ϕ(m)求出因子即可,从小到大依次判断是不是符合ad = 1(mod m)(d是ϕ(m)的因子) 求最小的原根的方法: 根据性质8,对ϕ(m)求出素因子,从1开始不断测 ...

  5. Hibernate关于父类子类的映射

    怕忘记,把栗子贴上来 以Person类和Student类为例 public class Person { private Integer id; private String name; privat ...

  6. Hibernate多对一关联关系

    两个持久化类.Customer 和 OrderForm Customer 类. package com.zcd.hibernate.manyToOne; public class Customer { ...

  7. JavaScript小游戏--2048(PC端)

    1.初始化棋局 $(document).ready(function() { prepare_for_mobile(); //适配移动端 new_game(); }); 2.开始新游戏 functio ...

  8. 二十二、详述 IntelliJ IDEA 中恢复代码的方法

    在咱们正常开发项目的时候,难免遇到在开发过程中由于某种原因,想要将代码恢复到前一版本的情景.特别是在咱们删除了某些代码,想要恢复之前删除的代码的时候,了解这个在 IntelliJ IDEA 中恢复代码 ...

  9. javascript返回顶部插件+源码

    javascript插件->returnTop.js: /* ** 插件名称returnTop.js ** 调用返回头部单例参数说明 ** 调用方式:turn.init(ele,speed); ...

  10. Entity FreamWork 无法创建“System.Object”类型的常量值。此上下文仅支持基元类型或枚举类型错误解决

    Entity FreamWork 无法创建“System.Object”类型的常量值.此上下文仅支持基元类型或枚举类型错误解决: 最近在开发中把我原来抄的架构里面的主键由固定的Guid改成了可以泛型指 ...