mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 06:24:07 +00:00
day 9: 104-maximum-depth-of-binary-tree
This commit is contained in:
parent
df64aaced5
commit
01f1ea79b0
13
104-maximum-depth-of-binary-tree/driver.cpp
Normal file
13
104-maximum-depth-of-binary-tree/driver.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int main() {
|
||||
TreeNode n7 = TreeNode(7);
|
||||
TreeNode n15 = TreeNode(15);
|
||||
TreeNode n20 = TreeNode(20, &n15, &n7);
|
||||
TreeNode n9 = TreeNode(9);
|
||||
TreeNode n3 = TreeNode(3, &n9, &n20);
|
||||
int answer = 3;
|
||||
Solution soln;
|
||||
cout << "Found depth correctly? " << soln.test(&n3, answer) << endl;
|
||||
return 0;
|
||||
}
|
||||
18
104-maximum-depth-of-binary-tree/soln.cpp
Normal file
18
104-maximum-depth-of-binary-tree/soln.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int Solution::max(int a, int b)
|
||||
{
|
||||
return a > b ? a : b;
|
||||
}
|
||||
int Solution::maxDepth(TreeNode *root)
|
||||
{
|
||||
if (root == nullptr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
|
||||
}
|
||||
bool Solution::test(TreeNode *root, int answer)
|
||||
{
|
||||
return maxDepth(root) == answer;
|
||||
}
|
||||
19
104-maximum-depth-of-binary-tree/soln.hpp
Normal file
19
104-maximum-depth-of-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:
|
||||
int max(int a, int b);
|
||||
int maxDepth(TreeNode *root);
|
||||
bool test(TreeNode *root, int answer);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user