Skip to content

Commit cefa059

Browse files
committed
'Single Element in s Sorted Array' solved
1 parent d283a70 commit cefa059

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

source-code/Serialize_and_Deserialize_BST.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
88
* };
99
*/
10+
// Any serialize-deserialize technique working for BT will work for BST because BST is a BT but may not the other way around
1011
class Codec {
1112
void preorder(TreeNode* root, string& data) {
1213
if(!root) return;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int singleNonDuplicate(vector<int>& arr) {
4+
int n = (int)arr.size();
5+
int left = 0, right = n - 1;
6+
while(left < right) {
7+
int mid = left + (right - left) / 2;
8+
if(arr[mid] != arr[mid - 1] and arr[mid] != arr[mid + 1]) {
9+
return arr[mid];
10+
}
11+
12+
if((mid % 2 == 1 and arr[mid] == arr[mid - 1]) or
13+
(mid % 2 == 0 and arr[mid] == arr[mid + 1])) {
14+
left = mid + 1;
15+
} else {
16+
right = mid - 1;
17+
}
18+
}
19+
return arr[left];
20+
}
21+
};

0 commit comments

Comments
 (0)