LeetCode 104. Maximum Depth of Binary Tree - Interview Prep Ep 65

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


⭐ Support my channel and connect with me:

Solutions explained:
Recursive solution: We'll add one to each level before traversing to its left and right children, return zero when the current node is null.

Iterative solution: We can use two extra data structures to help us mimic what happens when the recursive program runs, one is the stack that holds the tree nodes that we'll iterate on, the other is the depths of each of the node.

// TOOLS THAT I USE:

// MY FAVORITE BOOKS:

My ENTIRE Programming Equipment and Computer Science Bookshelf:

And make sure you subscribe to my channel!

Your comments/thoughts/questions/advice will be greatly appreciated!

#softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures
Рекомендации по теме
Комментарии
Автор

Actually this is the best explanation you can find on youtube....Others just focused on writing the code.Thanks man!

slycreator
Автор

Thanks for explaining this so well. I feel like a lot of coding youtubers don't break down a problem into it's visual side which is so important for learning how something works!

TejaswiniManjunatha
Автор

Thank you, I was looking for exactly this type of diagrammatic explanation! Thank you so much!!!

yixuanniu
Автор

This was the best explanation I could find of this problem, I appreciate you drawing out the recursive calls.

cameronhoward
Автор

Level up! My algorithmic intelligence has been increased by 3.

mcreyfonacier
Автор

Excellent explanation. Thank you! Keep up the good work!

Patiencelad
Автор

That was the best explanation I've ever seen. Thank you very much!

adithyagowda
Автор

Thanks for such a detailed explanation, quite informative video.Please also explain code using C++.Please

ChandraShekhar-bycd
Автор

hey, thanks for the great explanation and effort Fisher. I hope you could possibly show a visual explanation for the iterative step as well for those who are not familiar with the LinkedList data structure. Thank you!

ILoveChocx
Автор

I finally understood recursion, thanks buddy

rakshith
Автор

Excellent explanation. Much appreciated!

tuozhang
Автор

that's a great explanation. Thanks !!

kaanhanokyay
Автор

you are amazing man, love your videos

qahoushh
Автор

Hey, I'm having trouble
I wrote the following code but not sure why it returns the wrong answer

public int maxDepth(TreeNode root)
{
if(root==null) return 0;

if(root.left!=null) lh = maxDepth(root.left);
if(root.right!=null) rh = maxDepth(root.right);

return 1+Math.max(lh, rh);

}

lakshaygupta
Автор

pretty good. BTW, I guess you can speak mandarin, right?

zhenliu
Автор

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 call maintain its own count variable

hydrocy.
welcome to shbcf.ru