1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
  13. vector<bool> visited(, false);
  14. vector<unordered_set<int>> g();
  15.  
  16. dfs(root, g);
  17.  
  18. vector<int> res;
  19. queue<int> q;
  20. q.push(target->val);
  21. visited[target->val] = true;
  22. int lv = ;
  23. while (!q.empty()) {
  24. int qs = q.size();
  25. if (lv == K) {
  26. while(!q.empty()) {
  27. res.push_back(q.front());
  28. q.pop();
  29. }
  30. break;
  31. }
  32. while (qs-- > ) {
  33. int p = q.front();
  34. q.pop();
  35. for (auto i : g[p]) {
  36. if (!visited[i]) {
  37. q.push(i);
  38. visited[i] = true;
  39. }
  40. }
  41. }
  42. lv++;
  43. }
  44. return res;
  45. }
  46. void dfs(TreeNode *root, vector<unordered_set<int>>& g) {
  47. if (root == NULL) return;
  48. if (root->left) {
  49. g[root->val].insert(root->left->val);
  50. g[root->left->val].insert(root->val);
  51. }
  52. if (root->right) {
  53. g[root->val].insert(root->right->val);
  54. g[root->right->val].insert(root->val);
  55. }
  56. dfs(root->left, g);
  57. dfs(root->right, g);
  58. }
  59. };

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. 30分钟学会JS AST,打造自己的编译器

    这是一棵树嘛 直奔主题 抽象语法树是js代码另一种结构映射,可以将js拆解成AST,也可以把AST转成源代码.这中间的过程就是我们的用武之地. 利用 抽象语法树(AST) 可以对你的源代码进行修改.优 ...

  2. [问题记录]cocos的lua绑定安装pyyaml报错

    描述:按照readme中的文档操作报错,提示python2.7没有安装,但是确实已经安装了,而且也设置环境变量成功了. 解决: 在D盘新建register.py的文件,内容如下: # # script ...

  3. 打包python到exe

    #!/usr/bin/python # -*- coding:utf-8 -*- import distutils import py2exe from distutils.core import s ...

  4. AutoHotkey调用VBA实现批量精确筛选数据透视表某字段内容。

    如上图,想在数据透视表中只显示红色区域的内容,手动勾选就比较繁琐. 实现思路: 先复制红色的内容. 鼠标停留在数据透视表[型号]列的任意数据上(通过该单元格可以获取数据透视表和字段) 由于数据透视表的 ...

  5. bep-10翻译

    dht协议的目的是解放tracter服务器,将tracter的任务分布式存到各个客户端上(即维护资源文件的下载列表,从哪能下载到请求的文件): dht协议在get_peer请求获得peer信息后,就会 ...

  6. nbu异地备份实施前,数据收集日志

    1.修改bp.conf配置文件显示重删率 BPDBJOBS_COLDEFS = JOBID 5 true BPDBJOBS_COLDEFS = TYPE 7 false BPDBJOBS_COLDEF ...

  7. js 页面 json对象转数组

    json_array(data); function json_array(data){ var len=eval(data).length; var arr=[]; for(var i=0;i< ...

  8. Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

    任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...

  9. java三大特性(封装、继承、多态)

    oop(面向对象程序设计)具有三大特性:封装.继承.多态 一.封装 封装就是讲类的信息隐藏在类的内部,不允许外部程序直接访问,而是通过该类的实现隐藏信息的操作和访问. 实现封装 1.需要修改属性的访问 ...

  10. HDU4825 Xor Sum

    题意 给定一个集合后, 求一组查询中每个数和集合中任一元素异或的最大值. 题解 异或的规律是这样的 1 ^ 1 = 0, 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 而最大值即是 ...