Maximum Depth of Binary Tree (LeetCode 104) | Full Solution with animations | Study Algorithms

preview_player
Показать описание
The underlying concept to finding the maximum depth is to find the height of a binary tree and is a very basic concept which is used in a lot of preliminary analysis. You can approach this problem using a recursive technique or an iterative technique. One can also see the height of a tree as the deepest level in a binary tree. This video takes advantage of the level order traversal technique to find the height of a binary tree.

Chapters:
00:00 - Intro
01:31 - Problem statement and description
03:34 - Brute Force approach to find the height of a binary tree
06:05 - Efficient method
09:19 - Dry-run of Code
14:18 - Final Thoughts

📚 Links to topics I talk about in the video:

📖 Reference Books:

🎥 My Recording Gear:

💻 Get Social 💻

#leetcode #programming #binarytrees
Рекомендации по теме
Комментарии
Автор

The way of starting with Brute Force is pretty good, most people think complex and end up of not having the basic solution.
Kudos to your work...

aspiretechie
Автор

You are great man! I am amazed the way you simply the problems.

Nishi-Tiwari
Автор

Finally, I understand this implementation. Thank you so much 🙏!!

hinocenciopaulo
Автор

Thankyou for making videos like this, plz make more specially all that blind 75

IshaZaka
Автор

Bro i have exam for amadeous next month plz help me in preparation in coding. you're the only one who taught me algorithms clearly.plzz bro😢😢

pradeeps
Автор

Loved your all the videos. But this code has one flaw. If you see the queue size will never be zero until it reached leaf node as we are enqueuing it simultaneously will children nodes, as a result height will never increment to its correct value.

mehnazahmad
Автор

Height of Binary tree is the number of edges along the longest path from root to leaf
Max Depth of Binary tree is the number of nodes along the longest path from root to leaf
Max Depth = Height + 1
So the answer should be 3 right for the example you have given

salmamohammed
Автор

how to calculate the distance between the root and each of the leaf nodes in the brute-force approach?

gokulnaathb
Автор

Height of Binary tree is the number of edges along the longest path from root to leaf
but why we adding 1 also leaf node.which is wrong correct?

def height(root):

# Base case: Empty tree has a height equals 0
if root is None:
return 0

# Calling Left and Right node recursively
return 1 + max(height(root.left), height(root.right))

RN-jozt
Автор

plz don't mind, this 14th and 11th from this playlist are the same right?

chiruchiruchiranjeevi
Автор

How does an TreeNode "element" represent an "int" if int value is stored in the element.val (int val) inside a TreeNode object? Are we not supposed to reference the int value with element.val?

ivanchl
Автор

any resource on building this basic type of binary tree? i find it harder to build it than binary search trees that split the values based on their size to left and right subtrees

niko
Автор

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

hydrocy.