mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 07:44:07 +00:00
day 27: 226-invert-binary-tree, 11-container-with-most-water
This commit is contained in:
parent
06ceedd3ca
commit
d9298b9600
10
11-container-with-most-water/driver.cpp
Normal file
10
11-container-with-most-water/driver.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
vector<int> heights = {1, 8, 6, 2, 5, 4, 8};
|
||||||
|
int answer = 40;
|
||||||
|
Solution soln;
|
||||||
|
cout << "Found largest container size correctly? " << soln.test(heights, answer) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
21
11-container-with-most-water/soln.cpp
Normal file
21
11-container-with-most-water/soln.cpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int Solution::maxArea(vector<int> &height)
|
||||||
|
{
|
||||||
|
int left = 0, right = height.size() - 1;
|
||||||
|
int max_water = 0;
|
||||||
|
while (left != right)
|
||||||
|
{
|
||||||
|
max_water = max(max_water, min(height[left], height[right]) * (right - left));
|
||||||
|
if (height[left] > height[right])
|
||||||
|
right--;
|
||||||
|
else
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
return max_water;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Solution::test(vector<int> &height, int answer)
|
||||||
|
{
|
||||||
|
return maxArea(height) == answer;
|
||||||
|
}
|
||||||
8
11-container-with-most-water/soln.hpp
Normal file
8
11-container-with-most-water/soln.hpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int maxArea(vector<int> &height);
|
||||||
|
bool test(vector<int> &height, int answer);
|
||||||
|
};
|
||||||
26
226-invert-binary-tree/driver.cpp
Normal file
26
226-invert-binary-tree/driver.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// input
|
||||||
|
TreeNode n1 = TreeNode(1);
|
||||||
|
TreeNode n3 = TreeNode(3);
|
||||||
|
TreeNode n6 = TreeNode(6);
|
||||||
|
TreeNode n9 = TreeNode(9);
|
||||||
|
TreeNode n2 = TreeNode(2, &n1, &n3);
|
||||||
|
TreeNode n7 = TreeNode(7, &n6, &n9);
|
||||||
|
TreeNode n4 = TreeNode(4, &n2, &n7);
|
||||||
|
// answer
|
||||||
|
TreeNode n_9 = TreeNode(9);
|
||||||
|
TreeNode n_6 = TreeNode(6);
|
||||||
|
TreeNode n_3 = TreeNode(3);
|
||||||
|
TreeNode n_1 = TreeNode(1);
|
||||||
|
TreeNode n_7 = TreeNode(7, &n_9, &n_6);
|
||||||
|
TreeNode n_2 = TreeNode(2, &n_3, &n_1);
|
||||||
|
TreeNode n_4 = TreeNode(4, &n_7, &n_2);
|
||||||
|
|
||||||
|
Solution soln;
|
||||||
|
TreeNode *solved_n4 = soln.invertTree(&n4);
|
||||||
|
cout << "Inverted binary tree correctly? " << soln.test(solved_n4, &n_4) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
31
226-invert-binary-tree/soln.cpp
Normal file
31
226-invert-binary-tree/soln.cpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
TreeNode *Solution::invertTree(TreeNode *root)
|
||||||
|
{
|
||||||
|
if (root != nullptr)
|
||||||
|
{
|
||||||
|
TreeNode *tmp = root->left;
|
||||||
|
root->left = root->right;
|
||||||
|
root->right = tmp;
|
||||||
|
invertTree(root->left);
|
||||||
|
invertTree(root->right);
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Solution::test(TreeNode *solved_root, TreeNode *answer)
|
||||||
|
{
|
||||||
|
if(solved_root == nullptr && answer == nullptr) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (solved_root != nullptr && answer != nullptr)
|
||||||
|
{
|
||||||
|
if (solved_root->val != answer->val)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Solution::test(solved_root->left, answer->left) && Solution::test(solved_root->right, answer->right);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
19
226-invert-binary-tree/soln.hpp
Normal file
19
226-invert-binary-tree/soln.hpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct TreeNode
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
TreeNode *left;
|
||||||
|
TreeNode *right;
|
||||||
|
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TreeNode *invertTree(TreeNode *root);
|
||||||
|
bool test(TreeNode *root, TreeNode *answer);
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user