-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCommonNodesBST.cpp
78 lines (69 loc) · 1.42 KB
/
CommonNodesBST.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
/*
Common Nodes in Two Binary Search Trees
Given two BSTs, return the sum of all common nodes in both. In case there is no common node, return 0 NOTE
1. Your code will run on multiple test cases, please come up with an optimised solution.
2. Try to do it one pass through the trees.
INPUT FORMAT
A : Root of Tree A
B : Root of Tree B
EXAMPLE INPUT
Tree A:
5
/ \
2 8
\ \
3 15
/
7
Tree B:
7
/ \
1 10
\ \
2 15
/
8
EXAMPLE OUTPUT
38
EXAMPLE EXPLANATION
Common Nodes are : 2, 7, 8, 15
So answer is 2 + 7 + 8 + 15 = 32
*/
// Traverse one BST store it in unordered_set, now for all elements of second BST if found in set add it to sum
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
unordered_set<int> us;
void preorderTraversal(TreeNode *A)
{
if (A == NULL)
return;
us.insert(A->val);
preorderTraversal(A->left);
preorderTraversal(A->right);
}
int returnSum(TreeNode *B)
{
if (B == NULL)
return 0;
int sum = 0;
int left = returnSum(B->left);
if (us.find(B->val) != us.end())
{
sum += B->val;
}
int right = returnSum(B->right);
return sum + left + right;
}
int Solution::solve(TreeNode *A, TreeNode *B)
{
us.clear();
preorderTraversal(A);
return returnSum(B);
}