-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClosestLeafInABinaryTree.cpp
76 lines (73 loc) · 2.16 KB
/
ClosestLeafInABinaryTree.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
/**
* 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:
void buildGraph(TreeNode * root, unordered_map<TreeNode *, vector<TreeNode *>>& graph){
if(root != nullptr){
if(root->left){
graph[root].push_back(root->left);
graph[root->left].push_back(root);
buildGraph(root->left, graph);
}
if(root->right){
graph[root].push_back(root->right);
graph[root->right].push_back(root);
buildGraph(root->right, graph);
}
}
}
TreeNode * find(TreeNode * root, int target){
if(root!= nullptr){
if(root->val == target){
return root;
}
TreeNode * left = find(root->left, target);
if(left != nullptr){
return left;
}
TreeNode * right = find(root->right, target);
if(right != nullptr){
return right;
}
}
return nullptr;
}
bool isLeaf(TreeNode * root){
return !root->left && !root->right;
}
int findClosestLeaf(TreeNode* root, int k) {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//construct a graph
unordered_map<TreeNode *, vector<TreeNode *>> graph;
buildGraph(root, graph);
TreeNode * start = find(root, k);
//applying bfs from starting node to find the first leaf node
queue<TreeNode *> q;
unordered_set<TreeNode *> vis;
q.push(start);
vis.insert(start);
while(!q.empty()){
auto curr = q.front();
if(isLeaf(curr)){
return curr->val;
}
q.pop();
for(auto next : graph[curr]){
if(vis.find(next) == vis.end()){
q.push(next);
vis.insert(next);
}
}
}
return 0;
}
};