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进行层次遍历即可. 这种解法 ...
随机推荐
- Windows2003建立FTP服务器以及报530 User <用户名> cannot log in home directory inaccessible的解决方法
Windows2003建立FTP服务器: Windows2003建立FTP服务器 FTP连接 ...
- 点击页面li显示li中文字
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 使用Python+Selenium过程中中常见的问题汇总
1.提示:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 542: ordinal not in range( ...
- ES6 rest与扩展运算符
1.rest 变量将多余的参数放入数组中. function add(...values) { let sum = 0; for (var val of values) { sum += val; } ...
- 【剑指Offer学习】【面试题37:两个链表的第一个公共结点】
题目:输入两个链表,找出它们的第一个公共结点. 链表结点定义 /** * 链表结点类 */ private static class ListNode { int val; ListNode next ...
- Shell脚本之:if-else
Shell 有三种 if ... else 语句: 1.if ... fi 语句: 2.if ... else ... fi 语句: 3.if ... elif ... else ... fi 语句. ...
- CSS环绕球体的旋转文字-3D效果
代码地址如下:http://www.demodashi.com/demo/12482.html 项目文件结构截图 只需要一个html文件既可: 项目截图: 代码实现原理: 该示例的实现过程很简单,主要 ...
- VueJS事件处理器v-on
事件监听可以使用 v-on 指令. v-on:click表达式 HTML: <!DOCTYPE html> <html> <head> <meta chars ...
- VueJS实现双向数据绑定:v-model
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- SQL Server外连接、内连接、交叉连接
小编在做组织部维护最后收尾工作的时候,遇到了这样一个问题,须要将定性考核得分查出来.定量考核相应的数据查出来并进行得分计算.附加分查出来,最后将这三部分信息汇总之后得到总成绩,假设当中一项成绩没有进行 ...