-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailycoding036.cpp
123 lines (98 loc) · 2.69 KB
/
dailycoding036.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
112
113
114
115
116
117
118
119
120
121
122
123
/*
* This problem was asked by Dropbox.
*
* Given the root to a binary search tree,
* find the second largest node in the tree.
*
* solution:
*
* Split the problem into two cases, the root has a right child, and
* the root does not have a right child.
* case1) Right child exists. In this case, travel down the right path
* only since the root is a BST, storing the parent along the way.
* then return the parent.
* case2) Right child does not exist. In this case, travel down the right
* starting from the root's left node, but this time, simply return
* the rightmost leaf from this process.
* Runs in O(h) time where h is the height of the tree, constant space.
*/
#include <iostream>
#include <map>
using std::map;
using std::cout;
using std::endl;
struct Node {
int val;
Node *left, *right;
};
Node* newNode(int val) {
Node* node = new Node;
node->val = val;
node->left = node->right = NULL;
return node;
}
class Solution {
public:
Node* findLargestOrParent(Node* node, bool returnParent) {
Node* parent = NULL;
while (node->right) {
parent = node;
node = node->right;
}
return (returnParent)?parent:node;
}
Node* findSecondLargest(Node* root) {
if (root == NULL)
return NULL;
if (root->left == NULL && root->right == NULL)
return root;
Node* second;
if (root->left && root->right == NULL)
second = findLargestOrParent(root->left, false);
else
second = findLargestOrParent(root->right, true);
return second;
}
bool testHandle(map<Node*, int> tests) {
map<Node*, int>::iterator it;
bool result = true;
for (it = tests.begin(); it != tests.end(); it++) {
result &= (findSecondLargest(it->first)->val == it->second);
}
return result;
}
};
int main() {
map<Node*, int> tests;
Solution sol;
Node* root1 = newNode(5);
tests[root1] = 5;
Node* root2 = newNode(6);
root2->left = newNode(4);
root2->left->left = newNode(2);
tests[root2] = 4;
Node* root3 = newNode(6);
root3->left = newNode(3);
root3->left->right = newNode(4);
tests[root3] = 4;
Node* root4 = newNode(3);
root4->right = newNode(4);
root4->right->right = newNode(5);
tests[root4] = 4;
Node* root5 = newNode(3);
root5->right = newNode(6);
root5->right->left = newNode(4);
root5->right->right = newNode(7);
tests[root5] = 6;
Node* root6 = newNode(1);
root6->right = newNode(2);
root6->right->right = newNode(3);
root6->right->right->right = newNode(4);
root6->right->right->right->right = newNode(5);
tests[root6] = 4;
if (sol.testHandle(tests))
cout << "Passed" << endl;
else
cout << "Failed" << endl;
return 0;
}