Maximum Depth of Binary Tree | Leetcode 104 | Amazon interview question

preview_player
Показать описание
✅ Useful Links

Leetcode 112 Maximum Depth of Binary Tree

COURSES 📚
10% ᴅɪꜱᴄᴏᴜɴᴛ ᴏɴ ɢᴇᴇᴋꜱꜰᴏʀɢᴇᴇᴋꜱ ᴄᴏᴜʀꜱᴇꜱ ᴜꜱᴇ ᴄᴏᴅᴇ ᴘʀᴀᴋᴀꜱʜ10

Problem statement on leetcode -
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Рекомендации по теме
Комментарии
Автор

int maxDepth(TreeNode* root) {
int maxDepth = 0; // Initialize the maximum depth
int count = 0; // Initialize the current depth counter
dfs(root, count, maxDepth);
return maxDepth;
}

private:
void dfs(TreeNode* node, int count, int &maxDepth) {
if (node == NULL) return;
count++; // Increment counter to reflect current depth
if (count > maxDepth) {
maxDepth = count; // Update maximum depth
}
dfs(node->left, count, maxDepth);
dfs(node->right, count, maxDepth);
}
};

brother this code works but i cant understand how every recursion has its own count variable

hydrocy.
welcome to shbcf.ru