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

  Рет қаралды 12,378

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Пікірлер: 33
@aspiretechie1191
@aspiretechie1191 3 ай бұрын
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...
@Satya-g5t
@Satya-g5t Ай бұрын
😄 really like this solution instead of recursive stuff. good solution.
@Nishi-Tiwari
@Nishi-Tiwari Жыл бұрын
You are great man! I am amazed the way you simply the problems.
@neeraj_m
@neeraj_m Ай бұрын
Great solution! Btw, we don't need the if condition inside while loop - public int maxDepth(TreeNode root) { if (root == null) { return 0; } Queue queue = new LinkedList(); queue.add(root); int level = 0; while (!queue.isEmpty()) { int nNodes = queue.size(); while(nNodes > 0) { TreeNode node = queue.poll(); if (node.left != null) { queue.add(node.left); } if (node.right != null) { queue.add(node.right); } nNodes--; } level++; } return level; }
@IshaZaka
@IshaZaka 4 ай бұрын
Thankyou for making videos like this, plz make more specially all that blind 75
@hinocenciopaulo
@hinocenciopaulo Жыл бұрын
Finally, I understand this implementation. Thank you so much 🙏!!
@pradeeps9633
@pradeeps9633 2 жыл бұрын
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😢😢
@gauravmishra8782
@gauravmishra8782 11 ай бұрын
nice video nikhil sir!!!
@salmamohammed1200
@salmamohammed1200 2 жыл бұрын
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
@nikoo28
@nikoo28 2 жыл бұрын
These terms will be used interchangeably…but you get the correct idea 👍
@chiruchiruchiranjeevi3237
@chiruchiruchiranjeevi3237 Жыл бұрын
@@nikoo28 bor even my test cases are not passing its showing wrong, please do help me in this once..
@brinderdhaliwal3570
@brinderdhaliwal3570 Жыл бұрын
@@chiruchiruchiranjeevi3237 You probably have to check for an empty tree (root == null)
@honey-xr5kp
@honey-xr5kp 9 ай бұрын
all you gotta do is change the numberOfLevels to be initialized as 0 instead of -1 and it should all work. 0 for depth, -1 for height.@@chiruchiruchiranjeevi3237
@mehnazahmad7929
@mehnazahmad7929 8 ай бұрын
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.
@nikoo28
@nikoo28 8 ай бұрын
It is always the correct value. The code passes all cases on LeetCode. Can you elaborate more?
@RN-jo8zt
@RN-jo8zt 10 ай бұрын
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))
@nikoo28
@nikoo28 10 ай бұрын
i did not understand your question. The 1 actually accounts for your count. If you do not have that..the result will always be 0
@gokulnaathb2627
@gokulnaathb2627 10 ай бұрын
how to calculate the distance between the root and each of the leaf nodes in the brute-force approach?
@nikoo28
@nikoo28 10 ай бұрын
you keep on traversing each direction, until you find the desired leaf node
@alekseiwe
@alekseiwe Жыл бұрын
Thank man!
@startercoder
@startercoder Жыл бұрын
Helpful man!
@nikoo28
@nikoo28 Жыл бұрын
Glad to hear it!
@ivanchl
@ivanchl 8 ай бұрын
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?
@nikoo28
@nikoo28 8 ай бұрын
What do you mean? If the element.val is int, it will be of the int type. Can you clarify your question please
@ivanchl
@ivanchl 8 ай бұрын
@@nikoo28 Thank you so much for replying! I will give an example from your code: if you add to queue elementQueue.add(root), isn't it supposed to be elementQueue.add(root.val)? How does it retrieve the integer from just a "root" TreeNode object if we do not reference exactly to the value(root.val)? public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} ....... etc.
@nikoo28
@nikoo28 5 ай бұрын
that is because if you retrieve the root, you will get a TreeNode object. you can then fetch the value using ".val"
@chiruchiruchiranjeevi3237
@chiruchiruchiranjeevi3237 Жыл бұрын
plz don't mind, this 14th and 11th from this playlist are the same right?
@ashok2089
@ashok2089 6 ай бұрын
Yes. His intention is to convey that the same problem can be asked in two ways: Maximum Depth of Binary Tree, Height of binary tree.
@subee128
@subee128 Жыл бұрын
Thanks
@ArjunKumar-xh2xo
@ArjunKumar-xh2xo 5 ай бұрын
Thank you !!! :)
@hydrocy.9165
@hydrocy.9165 6 ай бұрын
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
@niko9338
@niko9338 6 ай бұрын
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
28- شرح الـ Binary Tree Traversal | BFS, DFS, Preorder, Inorder, Postorder
14:26
محمود سامي Hard-Code l
Рет қаралды 92 М.
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
Pacific Atlantic Water Flow - Leetcode 417 - Python
16:28
NeetCode
Рет қаралды 200 М.
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 272 М.
I made Tetris in C, this is what I learned
15:15
Austin Larsen
Рет қаралды 27 М.
Binary Tree Algorithms for Technical Interviews - Full Course
1:48:53
freeCodeCamp.org
Рет қаралды 745 М.