LeetCode 863. All Nodes Distance K in Binary Tree
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
.
Solution
// Solution 1: O(n) Time, O(n) Space
/**
* 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> ans;
unordered_map<TreeNode*, TreeNode*> parent; // son=>parent
unordered_set<TreeNode*> visit; //record visied node
void findParent(TreeNode* node) {
if (node == nullptr) return;
if (node->left != nullptr) {
parent[node->left] = node;
findParent(node->left);
}
if (node->right != nullptr){
parent[node->right] = node;
findParent(node->right);
}
}
void dfs(TreeNode* node, int K){
if (node == nullptr || visit.count(node) > 0) {
return;
}
visit.insert(node);
if (K == 0) {
ans.push_back(node->val);
return;
}
dfs(node->left, K-1);
dfs(node->right, K-1);
dfs(parent[node], K-1);
}
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
findParent(root);
dfs(target, K);
return ans;
}
};
// Solution 2: O(n) Time, O(lgn) Space
/**
* 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 {
bool FindPath(TreeNode* root, TreeNode* target, vector<bool>& paths) {
if (root == nullptr) {
return false;
}
if (target == root) {
return true;
}
bool l = FindPath(root->left, target, paths);
if (l) {
paths.push_back(false);
return true;
}
bool r = FindPath(root->right, target, paths);
if (r) {
paths.push_back(true);
return true;
}
return false;
}
void FindAnswer(TreeNode* root, vector<int>& ans, int dis, int K) {
if (root == nullptr || dis > K) {
return;
}
if (dis == K) {
ans.push_back(root->val);
return;
}
FindAnswer(root->left, ans, dis + 1, K);
FindAnswer(root->right, ans, dis + 1, K);
}
public:
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
vector<bool> paths;
FindPath(root, target, paths);
reverse(paths.begin(), paths.end());
vector<int> ans;
for (int i = 0; i < paths.size(); i++) {
if (paths.size() - i == K) {
ans.push_back(root->val);
}
bool path = paths[i];
if (path == false) {
FindAnswer(root->right, ans, paths.size() + 1 - i, K);
root = root->left;
} else {
FindAnswer(root->left, ans, paths.size() + 1 - i, K);
root = root->right;
}
}
FindAnswer(root, ans, 0, K);
return ans;
}
};
Last updated
Was this helpful?