863. All Nodes Distance K in Binary Tree
- /**
- * 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的更多相关文章
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- 距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...
随机推荐
- 30分钟学会JS AST,打造自己的编译器
这是一棵树嘛 直奔主题 抽象语法树是js代码另一种结构映射,可以将js拆解成AST,也可以把AST转成源代码.这中间的过程就是我们的用武之地. 利用 抽象语法树(AST) 可以对你的源代码进行修改.优 ...
- [问题记录]cocos的lua绑定安装pyyaml报错
描述:按照readme中的文档操作报错,提示python2.7没有安装,但是确实已经安装了,而且也设置环境变量成功了. 解决: 在D盘新建register.py的文件,内容如下: # # script ...
- 打包python到exe
#!/usr/bin/python # -*- coding:utf-8 -*- import distutils import py2exe from distutils.core import s ...
- AutoHotkey调用VBA实现批量精确筛选数据透视表某字段内容。
如上图,想在数据透视表中只显示红色区域的内容,手动勾选就比较繁琐. 实现思路: 先复制红色的内容. 鼠标停留在数据透视表[型号]列的任意数据上(通过该单元格可以获取数据透视表和字段) 由于数据透视表的 ...
- bep-10翻译
dht协议的目的是解放tracter服务器,将tracter的任务分布式存到各个客户端上(即维护资源文件的下载列表,从哪能下载到请求的文件): dht协议在get_peer请求获得peer信息后,就会 ...
- nbu异地备份实施前,数据收集日志
1.修改bp.conf配置文件显示重删率 BPDBJOBS_COLDEFS = JOBID 5 true BPDBJOBS_COLDEFS = TYPE 7 false BPDBJOBS_COLDEF ...
- js 页面 json对象转数组
json_array(data); function json_array(data){ var len=eval(data).length; var arr=[]; for(var i=0;i< ...
- Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】
任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...
- java三大特性(封装、继承、多态)
oop(面向对象程序设计)具有三大特性:封装.继承.多态 一.封装 封装就是讲类的信息隐藏在类的内部,不允许外部程序直接访问,而是通过该类的实现隐藏信息的操作和访问. 实现封装 1.需要修改属性的访问 ...
- HDU4825 Xor Sum
题意 给定一个集合后, 求一组查询中每个数和集合中任一元素异或的最大值. 题解 异或的规律是这样的 1 ^ 1 = 0, 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 而最大值即是 ...