-
Notifications
You must be signed in to change notification settings - Fork 0
/
6th February 2023
64 lines (34 loc) · 946 Bytes
/
6th February 2023
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
class Solution{
public:
Node* search(Node* root,int target)
{
if(!root || root->data==target) return root;
else if(root->data<target)
{
return search(root->right,target);
}
else return search(root->left,target);
}
// for getting vertical traversal
void vertical_order(Node* root, int idx,map<int,vector<int>>&m)
{
if(root)
{
m[idx].push_back(root->data);
vertical_order(root->left,idx-1,m);
vertical_order(root->right,idx+1,m);
}
}
long long int verticallyDownBST(Node *root,int target){
Node* found=search(root,target);
if(found==NULL) return -1;
map<int,vector<int>> m;
vertical_order(found,0,m);
long long ans=0;
for(int i=1;i<m[0].size();i++)
{
ans+=m[0][i];
}
return ans;
}
};