/**
* 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. WCF服务上应用protobuf z

    protobuf是google提供的一个开源序列化框架,类似于XML,JSON这样 的数据表示语言,其最大的特点是基于二进制,因此比传统的XML表示高效短小得多.虽然是二进制数据格式,但并没有因此变得 ...

  2. QT网络编程Tcp下C/S架构的即时通信

    先写一个客户端,实现简单的,能加入聊天,以及加入服务器的界面. #ifndef TCPCLIENT_H #define TCPCLIENT_H #include <QDialog> #in ...

  3. base64编码加密图片和展示图片

    base64是当前网络上最为常见的传输8Bit字节代码的编码方式其中之一.base64主要不是加密,它主要的用途是把某些二进制数转成普通字符用于 网络传输.由于这些二进制字符在传输协议中属于控制字符, ...

  4. TCP、UDP、Socket 通信(原)

    说明:本随笔主要演示自己给自己发送消息例子,分别使用了TCP协议.UDP协议以及socket套接字通信.使用socket套接字了模拟TCP.UDP通信实现原理.其中有些源码都来自<C#高级编程 ...

  5. ZT 类与类之间的四种关系

    csdn上一个好贴子:http://bbs.csdn.net/topics/390646332 类与类之间的四种关系1.依赖(Dependency)   类A在类B中作为一个成员函数的参数或者是返回值 ...

  6. IntelliJ IDEA设置编码格式

    IntelliJ IDEA设置编码格式为UTF-8

  7. 有趣的IntegerCache

    一个很有趣的现象,下面这两个结果输出的结果是false true,这是为什么?   翻看Integer的源码可以看到,当new Integer(12);时,没有什么特别的,就是通过构造方法创建了一个I ...

  8. 代码阅读:AFNetworking背后的思想

    1.一切皆文件:流与操作封装. 2.通信会话:

  9. Java 学习笔记1

    最近开始学习Java. <%@ page language="java" import="java.util.*" pageEncoding=" ...

  10. Jmeter关于数据库的测试(mysql数据库)

    建立jdbc链接:创建JDBC Connection Configuration. 添加——配置元件——JDBC Connection configuration: 配置JDBC Connection ...