Add solution to 'a lot' of problems

This commit is contained in:
Arkaprabha Chakraborty
2022-08-01 13:28:06 +05:30
parent 20d44dfd5e
commit ccc376aea0
9 changed files with 180 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
#include <cstdint>
#include <iostream>
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:
void traverse(TreeNode *root, int curlvl, int &minCount) {
if (root == nullptr) {
return;
}
if (root->left == nullptr && root->right == nullptr && curlvl < minCount) {
minCount = curlvl;
}
traverse(root->left, curlvl + 1, minCount);
traverse(root->right, curlvl + 1, minCount);
}
int minDepth(TreeNode *root) {
if (root == nullptr) {
return 0;
}
int minCount = INT32_MAX;
traverse(root, 1, minCount);
return minCount;
}
};