feat: 104_maximum_depth_of_binary_tree

This commit is contained in:
SquidSpirit 2025-03-05 23:54:24 +08:00
parent e07011a229
commit 4f2d4f58de

View File

@ -0,0 +1,35 @@
#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 maxDepth(TreeNode *root) {
unordered_map<TreeNode *, bool> visited;
int maxDepth = 0;
function<void(TreeNode *, int)> dfs = [&](TreeNode *root, int currentDepth) {
if (!root) return;
if (visited[root]) return;
visited[root] = true;
maxDepth = max(maxDepth, currentDepth);
dfs(root->left, currentDepth + 1);
dfs(root->right, currentDepth + 1);
};
dfs(root, 1);
return maxDepth;
}
};