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 list of the values of all nodes that have a distance K from the target node. The answer can be returned in any order.
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2
Output: [7,4,1]
Explanation:
The nodes that are a distance 2 from the target node (with value 5)
have values 7, 4, and 1.

Note that the inputs "root" and "target" are actually TreeNodes.
The descriptions of the inputs above are just serializations of these objects.
Note:
The given tree is non-empty.
Each node in the tree has unique values 0 <= node.val <= 500.
The target node is a node in the tree.
0 <= K <= 1000.
思路:将二叉树转换为无向图,然后进行dfs记录深度。 注意,对无向图进行遍历的时候要记录父节点,又不会有重复。
/**
* 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:
map<int, vector<int>> edge;
vector<int> ans;
void dfs(TreeNode* root) {
if (root == nullptr) return;
if (root->left != nullptr) {
dfs(root->left);
edge[root->val].push_back(root->left->val);
edge[root->left->val].push_back(root->val);
}
if (root->right != nullptr) {
dfs(root->right);
edge[root->val].push_back(root->right->val);
edge[root->right->val].push_back(root->val);
}
}
void dfs2(int u, int fa, int dep, int K) {
if (dep == K) {
ans.push_back(u);
return;
}
for (int i = 0; i < edge[u].size(); ++i) {
int v = edge[u][i];
if (v == fa) continue;
dfs2(v, u, dep+1, K);
}
}
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
dfs(root);
dfs2(target->val, -1, 0, K);
return ans;
}
};
leetcode 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 ...
- [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 ...
- 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 ...
- [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 ...
- 863. All Nodes Distance K in Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- [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 ...
- 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 ...
- [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 ...
- 距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...
随机推荐
- IOError: No translation files found for default language zh-cn.
IOError: No translation files found for default language zh-cn. 检查 ...\Lib\site-packages\Django-1.1 ...
- 探秘 flex 上下文中神奇的自动 margin
为了引出本文的主题,先看看这个问题,最快水平垂直居中一个元素的方法是什么? 水平垂直居中也算是 CSS 领域最为常见的一个问题了,不同场景下的方法也各不相同,各有优劣.嗯,下面这种应该算是最便捷的了: ...
- appium 'WebDriver' object has no attribute 'keyevent'
这个问题是我自己犯二了,开头应该是from appium import webdriver,写成了from selenium import webdriver,也可以运行,就是不能使用appium中独 ...
- 【GLSL教程】(二)在OpenGL中使用GLSL 【转】
http://blog.csdn.net/racehorse/article/details/6616256 设置GLSL 这一节讲述在OpenGL中配置GLSL,假设你已经写好了顶点shader和像 ...
- Linux下不重启永久修改hostname
LINUX下不重启永久修改hostname 1.如果只是修改hostname可以通过如下命令 hostname newHostname 注意:这种修改方式只有当前有效,等服务器重启后hostnam ...
- Incorrect column count: expected 1, actual 2
List<Long> idList = queryForList("ass.pageQuery_sgIds", paramMap, Long.class); 报错:In ...
- koajs 项目实战(二)
此篇文章,接 koajs 项目实战(一)后写 (六)表单提交和参数接收 表单: <form method="post" action="./users/zhuce& ...
- php信号处理
pcntl pcntl_signal 信号注册函数 pcntl_alarm 指定秒数中断程序执行任务. 每次执行只会有一个定时器生效,若之前计时器还没结束就定义新定时器,会替代之前定时器并返回之前定时 ...
- linux下安装rabbitmq的rpm包问题记录
安装rabbitmq的文章和帖子多如牛毛,不管是官网还是各个博客,这里附个Rabbitmq官网安装Rpm包的链接, http://www.rabbitmq.com/install-rpm.html 不 ...
- Docker入门系列7 动态映射端口port mapping
为何想要动态映射端口呢? 因为刚开始run启动容器时,并不知道里面需要映射哪些端口,等容器已创建了,想映射端口. 当然可以通过先commit成镜像,然后再次run时指定端口,但会生成中间的镜像,对于有 ...