/**
* 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. centOS 7 简单设置(虚拟机)

    1.修改主机名 hostnamectl set-hostname 2.ssh连接慢的问题解决 vim /etc/ssh/sshd_config UseDNS=no GSSAPIAuthenticati ...

  2. 修改mysql允许主机访问的权限

    开启mysql的远程访问权限 默认mysql的用户是没有远程访问的权限的,因此当程序跟数据库不在同一台服务器上时,我们需要开启mysql的远程访问权限. 主流的有两种方法,改表法和授权法. 相对而言, ...

  3. ORACLE BACKUP AND RECOVERY

    ORACLE BACKUP AND RECOVERY http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/11g/r2/2day_ ...

  4. Jsp Cookie的创建与读取 标签: cookiejsp 2016-11-17 15:14 61人阅读 评论(0)

    Cookie.jsp <%@ page language="Java" import="java.util.*" pageEncoding="u ...

  5. 文本处理三剑客之 Sed ——一般编辑命令

    sed简介 sed (stream editor for filtering and transforming text) 是Linux上的文本处理三剑客之一,另外两个是grep和awk. sed又称 ...

  6. June 26th 2017 Week 26th Monday

    I am a little tempted, but so afraid to look in your eyes. 我对你有一点动心,却如此害怕看你的眼睛. Now that you are tem ...

  7. kali配置ip,更新源,更新签名

    以下是我在简单玩kali时候碰到的一些问题,然后找到的解决办法,能成功解决我当时碰到的问题,如果你有疑问或者碰到不能解决的问题可以在下面评论 ############################# ...

  8. 理解Underscore中的flatten函数

    最近是在所在实习公司的第一个sprint,有个朋友又请假了,所以任务比较重,一直这么久都没怎么更新了,这个周末赖了个床,纠结了一会儿决定还是继续写这个系列,虽然比较乏味,但是学到的东西还是很多的. 之 ...

  9. swagger使用二:swagger配置多个项目注释

    在项目中采用swagger测试接口,提供接口给其他人员都非常的方便. 在swagger默认配置中,默认只显示接口访问层中的注释,可是很多的参数说明都已经在实体层中了啊?(如下图)不可能再把实体层中的模 ...

  10. 在UML系统开发中有三个主要的模型

    http://www.cnblogs.com/Yogurshine/archive/2013/01/14/2859248.html 在UML系统开发中有三个主要的模型: 功能模型: 从用户的角度展示系 ...