LeetCode Maximum Width of a Binary Tree Explained - Java

  Рет қаралды 35,365

Nick White

Nick White

Күн бұрын

Пікірлер: 52
@nikostrongioglou9941
@nikostrongioglou9941 3 жыл бұрын
Initializing the position to 0 produces wrong ids for the leftmost nodes. It works for the case where the bottom level has only two nodes but I cannot understand how the rest of the test cases work. Your solution is valid once you initialize the position it to 1.
@treyawey3878
@treyawey3878 2 жыл бұрын
Initialising the position of the root with 1 is a trick used for heaps.
@suhasnayak4704
@suhasnayak4704 5 жыл бұрын
Nice video! Just a tip - I think it would be better if you can walk through your logic on a white board, instead of just typing out the code!
@aasimkhan3198
@aasimkhan3198 4 жыл бұрын
yeah
@sidddcloud
@sidddcloud 4 жыл бұрын
to explain the math line. . at any level. we have saved leftmostpos. . . then on each node at that level, we are doing (currpos - leftmosepos+1) . this will give the total nodes on that level till that node. so, when u will eventually get to the right most node till will give u the max width at that level.
@tarsala1995
@tarsala1995 4 жыл бұрын
All I hear is: 2 + 2 is 4 minus 1 that's 3. Quick mafs. And I can't focus anymore :D
@mahmoudhamad6986
@mahmoudhamad6986 4 жыл бұрын
For the testcase that contains multiple nulls and 0s make the position as a Double (both in function argument and hashmap) and when you return it return it as an Int
@kenzhang1448
@kenzhang1448 4 жыл бұрын
lol.......ehhhh watched twice finally understand. I suggest to do a search on computeifabsent to understand the full solution. This video is confusing but better than the leetcode discussion.
@ashishtank9747
@ashishtank9747 2 жыл бұрын
Organic chemistry in Background board.
@JSDev776
@JSDev776 3 жыл бұрын
There's actually a more elegant solution that uses a simple arrayList instead of hashmap, since we're doing left call first ,the first call of a depth will always be the left most pos in that depth.
@ayush03d
@ayush03d 2 жыл бұрын
Were you studying Organic Chemistry back there on the board before?
@AlbertKamu
@AlbertKamu 4 жыл бұрын
I think they've updated test suit for this one, since this solution won't work anymore. There is tree with around 1000 levels so it won't be enough any integer for positions.
@pranjalchoubey5929
@pranjalchoubey5929 4 жыл бұрын
This solution worked perfectly for me!
@harshitmakhija6817
@harshitmakhija6817 4 жыл бұрын
Thanks I find it nice and simple i appreciate your effort
@yannisb2178
@yannisb2178 4 жыл бұрын
Wrong explanation mate. In your code the labels are not 1,2,3 etc. They are per depth. So you have 0 for the leftmost node and 2^depth - 1 for the rightmost one, providing the root depth is 0. That's why this works. And you calculate the final max_width for each depth at the rightmost node so you basically subtract its position from the leftmost one and you add 1 because otherwise you would get only the middle nodes.
@sidddcloud
@sidddcloud 4 жыл бұрын
if you take root pos as 1 , you will get the number as he said in the video and it works too. . you can give any no to the root. left most node doesnt have to be at 0 and rightmost as 2 powered depth -1 . left just need to be at parent*2. and right at parent*2+1 . u can start with any number. he said 1 , 2 , 3 in the video bcuz he took root at 1 while explaining. (in code he took 0 , u can take it as any number). positions are not marked as per depth, they are marked relative to the root ( parent to be exact).
@yannisb2178
@yannisb2178 4 жыл бұрын
@@sidddcloud If you run the algorithm and start with position 0 you will have something like below 0 --------- depth 0 / \ 0 1 --------- depth 1 / \ / \ 0 1 2 3 --------- depth 2 / \ / \ / \ / \ 0 1 2 3 4 5 6 7 --------- depth 3 So max_width is rightmost-leftmost +1(so for depth 3 it's 7-0+1 = 8). It's essentially the same thing because you have the same difference but I'm saying what he coded which is different and it might confuse some beginners
@Subhasish8844
@Subhasish8844 3 жыл бұрын
To those who have not understand the above explanation hope the below helps Basically what he is doing is he is maintaining a map where the key is level and the value is the leftmost node position of that level. Now since he is doing a DFS, so he is always calling the left node and then the right node so it is guaranteed that the map would be always initialised with the leftmost value of that level first. Now for each node in recursion if he is seeing that for that level the entry is already there in map then he simply subtracts the value from that level in map(leftMost element of that level) from the position value of the current node and replaces the whole value in max_width if eligible
@mananvarma5944
@mananvarma5944 3 жыл бұрын
Hey thanks a lot man! This helped more than the video did.
@asrealmadrid97
@asrealmadrid97 4 жыл бұрын
Awesome, great solution, and intuitive too.
@prash11leo
@prash11leo 4 жыл бұрын
shouldn't the depth of the root be 1 initially?
@jugsma6676
@jugsma6676 4 жыл бұрын
Nick, you are complicating your solution. Here's the simpler version: def max_width(root, cache, level): if root == None: return True if root.val != None: if level in cache: cache[level] = cache[level]+1 else: cache[level] = 1 max_width(root.left, cache, level+1) max_width(root.right, cache, level+1) return cache[len(cache)]
@Subhasish8844
@Subhasish8844 3 жыл бұрын
You sure its working?? doesnt look like
@iamnoob7593
@iamnoob7593 4 жыл бұрын
Good problem.I did this using BFS.
@viniciusguimaraes117
@viniciusguimaraes117 Жыл бұрын
How?
@iamnoob7593
@iamnoob7593 Жыл бұрын
@@viniciusguimaraes117 check leetcode solution section
@DEEPAKGOYALnullRA
@DEEPAKGOYALnullRA 4 жыл бұрын
class Solution { public int widthOfBinaryTree(TreeNode root) { int[] result=new int[1]; maximumWidth(root,0,0,result); return result[0]; } public void maximumWidth(TreeNode root,int depth,int position,int[] result){ if(root==null) return; result[0]=Math.max(result[0],position+1); maximumWidth(root.left,depth+1,position*2,result); maximumWidth(root.right,depth+1,position*2+1,result); } } can anyone tell me what is the mistake in code
@supremoluminary
@supremoluminary 4 жыл бұрын
I was not able to understand the strategy. Instead of using hand gestures, I prefer diagrams.
@raghuroh1835
@raghuroh1835 5 жыл бұрын
Do not initialize max_width to 0;
@JDarkish
@JDarkish 3 жыл бұрын
How the fk am i supposed to solve this in a real interview, those rare methods like computeifabsent got me
@Subhasish8844
@Subhasish8844 3 жыл бұрын
that line is same as if(levelMap.get(level) == null) levelMap.put(level,position);
@DEEPAKGOYALnullRA
@DEEPAKGOYALnullRA 4 жыл бұрын
awesome concept i got from this video thanks man
@krishna100982
@krishna100982 4 жыл бұрын
waiting for 300plus leetcode solns...
@jiangtianfeng5971
@jiangtianfeng5971 4 жыл бұрын
genius....
@mridulghanshala700
@mridulghanshala700 5 жыл бұрын
i think u should pass 1 to the position variable
@HarishAmarnath
@HarishAmarnath 4 жыл бұрын
yes, i dont know how this works
@DEEPAKGOYALnullRA
@DEEPAKGOYALnullRA 4 жыл бұрын
0.. will only come otherwise it will give wrong answer its better to draw a tree for this code you will get why here 0 comes not 1. if you see in right call he is adding 1 every time this will play a important role in this code so you will get this only after tracing a tree.
@sidddcloud
@sidddcloud 4 жыл бұрын
@@DEEPAKGOYALnullRA u can take any number. eg if u take 10. left child will be 20, right at 21. width will still be 2. (21-20+1). now pos 10 sibling will be at 11. its child will be at 22, 23 ( exactly where u want it to be. .) so , on the next level. pos will be 20, 21, 22, 23. . So, it doesnt matter where u begin.
@sameerpande7567
@sameerpande7567 4 жыл бұрын
Just find the number of levels and raise it to the power of 2 TreeNode traverserLeftNode = Tree.root; TreeNode traverseRightNode = Tree.root; int leftNodeLevel = 0; int rightNodeLevel = 0; while (traverserLeftNode.getLeftChild() != null) { leftNodeLevel += 1; traverserLeftNode = traverserLeftNode.getLeftChild(); } while (traverseRightNode.getRightChild() != null) { rightNodeLevel += 1; traverseRightNode = traverseRightNode.getRightChild(); } int x = Math.max(leftNodeLevel, rightNodeLevel); System.out.println(Math.pow(2, x));
@sameer9368
@sameer9368 4 жыл бұрын
Position value should be 1....I think
@sameer9368
@sameer9368 4 жыл бұрын
In case,everytime 0 multiply by 2 , it will give the leftmost position value as 0 only
@DEEPAKGOYALnullRA
@DEEPAKGOYALnullRA 4 жыл бұрын
it is totally correct 0.. will only come otherwise it will give wrong answer its better to draw a tree for this code you will get why here 0 comes not 1. if you see in right call he is adding 1 every time this will play a important role in this code so you will get this only after tracing a tree.
@sidddcloud
@sidddcloud 4 жыл бұрын
@@DEEPAKGOYALnullRA u can take root value anything.
@atrichaturvedi6322
@atrichaturvedi6322 4 жыл бұрын
This should be a hard level problem
@enigma2886
@enigma2886 3 жыл бұрын
ikr :(
@ustapc
@ustapc 4 жыл бұрын
please use the whiteboard
L28. Maximum Width of Binary Tree | C++ | Java
22:41
take U forward
Рет қаралды 276 М.
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 237 М.
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 14 МЛН
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,2 МЛН
How to Fight a Gross Man 😡
00:19
Alan Chikin Chow
Рет қаралды 20 МЛН
How Much Tape To Stop A Lamborghini?
00:15
MrBeast
Рет қаралды 255 МЛН
LeetCode Check Completeness of a Binary Tree Explained - Java
6:35
LeetCode Search A 2D Matrix Solution Explained - Java
11:47
Nick White
Рет қаралды 39 М.
I Got Rejected (again)
9:43
Nick White
Рет қаралды 205 М.
LeetCode Range Sum of BST Explained - Java
7:16
Nick White
Рет қаралды 16 М.
Leetcode - Maximum Width of Binary Tree (Python)
7:08
Timothy H Chang
Рет қаралды 3,3 М.
Diameter Of Binary Tree ( Basic Approach) - JAVA
19:46
Coding Ninjas
Рет қаралды 26 М.
Maximum Width of Binary Tree | Leetcode 662| Binary Tree | Day-27
18:44
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 158 М.
Maximum Width of Binary Tree - Leetcode 662 - Python
11:38
NeetCodeIO
Рет қаралды 21 М.
Diameter of a binary tree | Leetcode #543
13:31
Techdose
Рет қаралды 61 М.
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 14 МЛН