-
Notifications
You must be signed in to change notification settings - Fork 69
/
k_binary_tree.cpp
111 lines (98 loc) Β· 2.73 KB
/
k_binary_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
// Definition for a binary tree node.
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void dfs(TreeNode* node, TreeNode* parent, unordered_map<TreeNode*, TreeNode*>& parentMap)
{
if (node)
{
parentMap[node] = parent;
dfs(node->left, node, parentMap);
dfs(node->right, node, parentMap);
}
}
vector<int> distanceK(TreeNode* root, TreeNode* target, int k)
{
unordered_map<TreeNode*, TreeNode*> parentMap;
dfs(root, nullptr, parentMap);
queue<TreeNode*> q;
q.push(target);
unordered_map<TreeNode*, bool> visited;
visited[target] = true;
int distance = 0;
// Perform a level-order traversal starting from the target node
while (!q.empty())
{
int levelSize = q.size();
if (distance == k)
{
vector<int> result;
while (!q.empty())
{
result.push_back(q.front()->val);
q.pop();
}
return result;
}
for (int i = 0; i < levelSize; i++)
{
TreeNode* current = q.front();
q.pop();
if (current->left && !visited[current->left])
{
q.push(current->left);
visited[current->left] = true;
}
if (current->right && !visited[current->right])
{
q.push(current->right);
visited[current->right] = true;
}
TreeNode* parent = parentMap[current];
if (parent && !visited[parent])
{
q.push(parent);
visited[parent] = true;
}
}
distance++;
}
return {};
}
int main()
{
TreeNode* root = new TreeNode(3);
root->left = new TreeNode(5);
root->right = new TreeNode(1);
root->left->left = new TreeNode(6);
root->left->right = new TreeNode(2);
root->right->left = new TreeNode(0);
root->right->right = new TreeNode(8);
root->left->right->left = new TreeNode(7);
root->left->right->right = new TreeNode(4);
TreeNode* target = root->left; // Target node with value 5
int k = 2;
vector<int> result = distanceK(root, target, k);
cout << "Output: [";
for (int i = 0; i < result.size(); i++)
{
cout << result[i];
if (i < result.size() - 1)
{
cout << ", ";
}
}
cout << "]" << endl;
return 0;
}
//OUTPUT
//Output: [7, 4, 1]