Skip to content

Commit 108ac5e

Browse files
committed
Adding solution of binary tree mirror image and rotation question.
## Fixing Issue #3540 ## Description This pull request implements the functionality for generating the mirror image of a binary tree and rotating a binary tree in C++. The changes include the addition of two main functions: one for creating the mirror image of a given binary tree and another for performing left and right rotations of the tree. These features enhance the data structures and algorithms (DSA) folder by providing essential tree manipulation techniques. ## Type of PR - [ ] Bug fix - [x] Feature enhancement - [ ] Documentation update - [ ] Security enhancement - [ ] Other (specify): _______________ ## Checklist - [x] I have performed a self-review of my code. - [x] I have read and followed the Contribution Guidelines. - [x] I have tested the changes thoroughly before submitting this pull request. - [x] I have provided relevant issue numbers, screenshots, and videos after making the changes. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have followed the code style guidelines of this project. - [x] I have checked for any existing open issues that my pull request may address. - [x] I have ensured that my changes do not break any existing functionality. - [x] Each contributor is allowed to create a maximum of 4 issues per day. This helps us manage and address issues efficiently. - [x] I have read the resources for guidance listed below. - [x] I have followed security best practices in my code changes. ## Additional Context The implementation includes two main features for binary trees: 1. **Mirror Image of Binary Tree:** This function recursively swaps the left and right children of each node in the binary tree, producing a mirror image. 2. **Binary Tree Rotation:** Functions for performing left and right rotations of the binary tree. These rotations are crucial for balancing operations in certain types of binary trees, such as AVL trees. ## Resources for Guidance Please read the following resources before submitting your contribution: - [x] [Code Harbor Hub Community Features](https://www.codeharborhub.live/community/features) - [x] [Markdown Guide](https://www.markdownguide.org/)
1 parent 951b1e7 commit 108ac5e

File tree

2 files changed

+388
-172
lines changed

2 files changed

+388
-172
lines changed
Lines changed: 172 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,172 @@
1-
---
2-
id: reverse-pairs
3-
title: Reverse Pairs
4-
sidebar_label: 0493 - Reverse Pairs
5-
tags:
6-
- Leetcode
7-
- Merge Sort
8-
---
9-
10-
## Problem Statement
11-
12-
Given an integer array `nums`, return the number of reverse pairs in the array.
13-
14-
A reverse pair is defined as a pair `(i, j)` where:
15-
16-
- $0 <= i < j < nums.length and$
17-
- $nums[i] > 2 * nums[j].$
18-
19-
## Examples
20-
21-
### Example 1
22-
23-
- Input: `nums = [1,3,2,3,1]`
24-
- Output: `2`
25-
- Explanation: The reverse pairs are:
26-
- `(1, 4)` -> `nums[1] = 3`, `nums[4] = 1`, `3 > 2 * 1`
27-
- `(3, 4)` -> `nums[3] = 3`, `nums[4] = 1`, `3 > 2 * 1`
28-
29-
### Example 2
30-
31-
- Input: `nums = [2,4,3,5,1]`
32-
- Output: `3`
33-
- Explanation: The reverse pairs are:
34-
- `(1, 4)` -> `nums[1] = 4`, `nums[4] = 1`, `4 > 2 * 1`
35-
- `(2, 4)` -> `nums[2] = 3`, `nums[4] = 1`, `3 > 2 * 1`
36-
- `(3, 4)` -> `nums[3] = 5`, `nums[4] = 1`, `5 > 2 * 1`
37-
38-
## Constraints
39-
40-
- $1 <= nums.length <= 5 * 104$
41-
- $-231 <= nums[i] <= 231 - 1$
42-
43-
## Algorithm
44-
45-
To solve this problem efficiently, we can use a modified merge sort algorithm, which not only sorts the array but also counts the number of reverse pairs during the merging process. The idea is to leverage the divide-and-conquer approach to count pairs in `O(n log n)` time complexity.
46-
47-
### Steps:
48-
49-
1. **Divide**: Split the array into two halves.
50-
2. **Count**: Count the reverse pairs in each half recursively.
51-
3. **Merge and Count**: While merging the two halves, count the reverse pairs where one element is from the left half and the other is from the right half.
52-
53-
## Code
54-
55-
### Python
56-
57-
```python
58-
class Solution:
59-
def reversePairs(self, nums: List[int]) -> int:
60-
def merge_sort(nums, l, r):
61-
if l >= r:
62-
return 0
63-
64-
mid = (l + r) // 2
65-
count = merge_sort(nums, l, mid) + merge_sort(nums, mid + 1, r)
66-
67-
j = mid + 1
68-
for i in range(l, mid + 1):
69-
while j <= r and nums[i] > 2 * nums[j]:
70-
j += 1
71-
count += j - (mid + 1)
72-
73-
nums[l:r + 1] = sorted(nums[l:r + 1])
74-
return count
75-
76-
return merge_sort(nums, 0, len(nums) - 1)
77-
```
78-
79-
### C++
80-
81-
```cpp
82-
#include <vector>
83-
using namespace std;
84-
85-
class Solution {
86-
public:
87-
int reversePairs(vector<int>& nums) {
88-
return reversePairsSub(nums, 0, nums.size() - 1);
89-
}
90-
91-
private:
92-
int reversePairsSub(vector<int>& nums, int l, int r) {
93-
if (l >= r) return 0;
94-
95-
int m = l + ((r - l) >> 1);
96-
int res = reversePairsSub(nums, l, m) + reversePairsSub(nums, m + 1, r);
97-
98-
int i = l, j = m + 1, k = 0, p = m + 1;
99-
vector<int> merge(r - l + 1);
100-
101-
while (i <= m) {
102-
while (p <= r && nums[i] > 2L * nums[p]) p++;
103-
res += p - (m + 1);
104-
105-
while (j <= r && nums[i] >= nums[j]) merge[k++] = nums[j++];
106-
merge[k++] = nums[i++];
107-
}
108-
109-
while (j <= r) merge[k++] = nums[j++];
110-
111-
copy(merge.begin(), merge.end(), nums.begin() + l);
112-
113-
return res;
114-
}
115-
};
116-
```
117-
118-
### Java
119-
120-
```java
121-
import java.util.Arrays;
122-
123-
class Solution {
124-
public int reversePairs(int[] nums) {
125-
return mergeSort(nums, 0, nums.length - 1);
126-
}
127-
128-
private int mergeSort(int[] nums, int left, int right) {
129-
if (left >= right) return 0;
130-
131-
int mid = left + (right - left) / 2;
132-
int count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right);
133-
134-
int j = mid + 1;
135-
for (int i = left; i <= mid; i++) {
136-
while (j <= right && nums[i] > 2L * nums[j]) j++;
137-
count += j - (mid + 1);
138-
}
139-
140-
Arrays.sort(nums, left, right + 1);
141-
return count;
142-
}
143-
}
144-
```
145-
146-
### JavaScript
147-
148-
```javascript
149-
var reversePairs = function (nums) {
150-
function mergeSort(nums, left, right) {
151-
if (left >= right) return 0;
152-
153-
let mid = left + Math.floor((right - left) / 2);
154-
let count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right);
155-
156-
let j = mid + 1;
157-
for (let i = left; i <= mid; i++) {
158-
while (j <= right && nums[i] > 2 * nums[j]) j++;
159-
count += j - (mid + 1);
160-
}
161-
162-
nums.splice(
163-
left,
164-
right - left + 1,
165-
...nums.slice(left, right + 1).sort((a, b) => a - b)
166-
);
167-
return count;
168-
}
169-
170-
return mergeSort(nums, 0, nums.length - 1);
171-
};
172-
```
1+
---
2+
id: reverse-pairs
3+
title: Reverse Pairs
4+
sidebar_label: 0493 - Reverse Pairs
5+
tags:
6+
- Leetcode
7+
- Merge Sort
8+
---
9+
10+
## Problem Statement
11+
12+
Given an integer array `nums`, return the number of reverse pairs in the array.
13+
14+
A reverse pair is defined as a pair `(i, j)` where:
15+
16+
- $0 <= i < j < nums.length and$
17+
- $nums[i] > 2 * nums[j].$
18+
19+
## Examples
20+
21+
### Example 1
22+
23+
- Input: `nums = [1,3,2,3,1]`
24+
- Output: `2`
25+
- Explanation: The reverse pairs are:
26+
- `(1, 4)` -> `nums[1] = 3`, `nums[4] = 1`, `3 > 2 * 1`
27+
- `(3, 4)` -> `nums[3] = 3`, `nums[4] = 1`, `3 > 2 * 1`
28+
29+
### Example 2
30+
31+
- Input: `nums = [2,4,3,5,1]`
32+
- Output: `3`
33+
- Explanation: The reverse pairs are:
34+
- `(1, 4)` -> `nums[1] = 4`, `nums[4] = 1`, `4 > 2 * 1`
35+
- `(2, 4)` -> `nums[2] = 3`, `nums[4] = 1`, `3 > 2 * 1`
36+
- `(3, 4)` -> `nums[3] = 5`, `nums[4] = 1`, `5 > 2 * 1`
37+
38+
## Constraints
39+
40+
- $1 <= nums.length <= 5 * 104$
41+
- $-231 <= nums[i] <= 231 - 1$
42+
43+
## Algorithm
44+
45+
To solve this problem efficiently, we can use a modified merge sort algorithm, which not only sorts the array but also counts the number of reverse pairs during the merging process. The idea is to leverage the divide-and-conquer approach to count pairs in `O(n log n)` time complexity.
46+
47+
### Steps:
48+
49+
1. **Divide**: Split the array into two halves.
50+
2. **Count**: Count the reverse pairs in each half recursively.
51+
3. **Merge and Count**: While merging the two halves, count the reverse pairs where one element is from the left half and the other is from the right half.
52+
53+
## Code
54+
55+
### Python
56+
57+
```python
58+
class Solution:
59+
def reversePairs(self, nums: List[int]) -> int:
60+
def merge_sort(nums, l, r):
61+
if l >= r:
62+
return 0
63+
64+
mid = (l + r) // 2
65+
count = merge_sort(nums, l, mid) + merge_sort(nums, mid + 1, r)
66+
67+
j = mid + 1
68+
for i in range(l, mid + 1):
69+
while j <= r and nums[i] > 2 * nums[j]:
70+
j += 1
71+
count += j - (mid + 1)
72+
73+
nums[l:r + 1] = sorted(nums[l:r + 1])
74+
return count
75+
76+
return merge_sort(nums, 0, len(nums) - 1)
77+
```
78+
79+
### C++
80+
81+
```cpp
82+
#include <vector>
83+
using namespace std;
84+
85+
class Solution {
86+
public:
87+
int reversePairs(vector<int>& nums) {
88+
return reversePairsSub(nums, 0, nums.size() - 1);
89+
}
90+
91+
private:
92+
int reversePairsSub(vector<int>& nums, int l, int r) {
93+
if (l >= r) return 0;
94+
95+
int m = l + ((r - l) >> 1);
96+
int res = reversePairsSub(nums, l, m) + reversePairsSub(nums, m + 1, r);
97+
98+
int i = l, j = m + 1, k = 0, p = m + 1;
99+
vector<int> merge(r - l + 1);
100+
101+
while (i <= m) {
102+
while (p <= r && nums[i] > 2L * nums[p]) p++;
103+
res += p - (m + 1);
104+
105+
while (j <= r && nums[i] >= nums[j]) merge[k++] = nums[j++];
106+
merge[k++] = nums[i++];
107+
}
108+
109+
while (j <= r) merge[k++] = nums[j++];
110+
111+
copy(merge.begin(), merge.end(), nums.begin() + l);
112+
113+
return res;
114+
}
115+
};
116+
```
117+
118+
### Java
119+
120+
```java
121+
import java.util.Arrays;
122+
123+
class Solution {
124+
public int reversePairs(int[] nums) {
125+
return mergeSort(nums, 0, nums.length - 1);
126+
}
127+
128+
private int mergeSort(int[] nums, int left, int right) {
129+
if (left >= right) return 0;
130+
131+
int mid = left + (right - left) / 2;
132+
int count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right);
133+
134+
int j = mid + 1;
135+
for (int i = left; i <= mid; i++) {
136+
while (j <= right && nums[i] > 2L * nums[j]) j++;
137+
count += j - (mid + 1);
138+
}
139+
140+
Arrays.sort(nums, left, right + 1);
141+
return count;
142+
}
143+
}
144+
```
145+
146+
### JavaScript
147+
148+
```javascript
149+
var reversePairs = function (nums) {
150+
function mergeSort(nums, left, right) {
151+
if (left >= right) return 0;
152+
153+
let mid = left + Math.floor((right - left) / 2);
154+
let count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right);
155+
156+
let j = mid + 1;
157+
for (let i = left; i <= mid; i++) {
158+
while (j <= right && nums[i] > 2 * nums[j]) j++;
159+
count += j - (mid + 1);
160+
}
161+
162+
nums.splice(
163+
left,
164+
right - left + 1,
165+
...nums.slice(left, right + 1).sort((a, b) => a - b)
166+
);
167+
return count;
168+
}
169+
170+
return mergeSort(nums, 0, nums.length - 1);
171+
};
172+
```

0 commit comments

Comments
 (0)