Maximum Depth of Binary Tree Leetcode 104. Learn BFS and DFS in 2 minutes with complete Code

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

Check out our other playlists:

Dynamic Programming:

Trees:

Heaps and Maps:

Arrays and Maths:

Bit Manipulation:

Greedy Algorithms:

Sorting and Searching:

Strings:

Linked Lists:

Stack and Queues:

Two Pointers:

Graphs, BFS, DFS:

Backtracking:

Non- DSA playlists:

Probability:

SQL-Basic Join functions:

SQL-Basic Aggregate functions:
Рекомендации по теме
Комментарии
Автор

BFS:

queue<TreeNode*>q;
if(root==NULL)return 0;
q.push(root);
int ans = 0;
while(!q.empty())
{ int n = q.size();
ans++;
for(int i=0;i<n;i++)
{
TreeNode* node = q.front();
q.pop();


}
}
return ans;

DFS:
if(!root)return 0;
return max(maxDepth(root->left), maxDepth(root->right))+1;

probabilitycodingisfunis
Автор

Mam please make video on DELETE AND EARN QUES IN LEETCODE(DYNAMIC PROGRAMMING).

harshmishra
Автор

plz tell me what we are adding in answer

muhammadyaseen
Автор

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);
}
}; this code works but i cant understand how every recursion call maintain its own count variable

hydrocy.