/**
* 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. CefSharp 浏览器核心,爬虫

    CefSharp是什么 A framework for embedding web-browsing-like capabilities to a standard .NET application ...

  2. Linux下安装方法总结(源码安装)

    很久之前安装过windows下以及Mac下的node,感觉还是很方便的,不成想今天安装Linux下的坑了老半天,特此记录. 首先去官网下载代码,这里一定要注意安装分两种,一种是Source Code源 ...

  3. SAP Cloud for Customer销售订单External Note的建模细节

    SAP Cloud for Customer的销售订单创建页面里,我们可以给一个订单维护External Note,当这个订单同步到S/4HANA生成对应的生产订单后,这个note可以作为备注提示生产 ...

  4. Intellij IDEA设置注释作者名字

    方法一:File >> Settings >> Editor >>Code Style >> File and Code Templates>&g ...

  5. Django 查询集的过滤内置条件

    条件选取querySet的时候,filter表示=,exclude表示!=.querySet.distinct() 去重复__exact 精确等于 like 'aaa' __iexact 精确等于 忽 ...

  6. 圆环,扇形控件基本算法一种实现 - 代码库 - CocoaChina_让移动开发更简单

    圆环,扇形控件基本算法一种实现 - 代码库 - CocoaChina_让移动开发更简单   ////  CircleCore.h//  Quartz////  Created by 仙人掌 on 12 ...

  7. BZOJ4337:[BJOI2015]树的同构(树hash)

    Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...

  8. MyBatis(3)开发dao方法

    本次全部学习内容:MyBatisLearning       SqlSession         SqlSession是一个面向用户(程序员)的接口.         SqlSession中提供了很 ...

  9. Mybatis 和Spring整合之mapper代理开发

    F:\1ziliao\mybatis\代码 1.1 SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8&quo ...

  10. PCA算法的最小平方误差解释

    PCA算法另外一种理解角度是:最小化点到投影后点的距离平方和. 假设我们有m个样本点,且都位于n维空间 中,而我们要把原n维空间中的样本点投影到k维子空间W中去(k<n),并使得这m个点到投影点 ...