Print the sum of every diagonal in binary tree. Print diagonal elements: • Print diagonal element...
Пікірлер: 29
@anandkulkarni21114 жыл бұрын
Try to add some animation to your videos, You approach to explanation and step wise derivation is one of the best on you tube for algorithm related problems.!!!
@vivekanandkhyade4 жыл бұрын
yess.... do u know someone who can help in making animations.. ?
@pradumanyadav38284 жыл бұрын
null point exception
@pradumanyadav38284 жыл бұрын
class Tree { public static ArrayList diagonalSum(Node root) { ArrayList List = new ArrayList(); path(root,List); return List; } public static void path(Node node,ArrayList lst){ Queue main = new ArrayDeque(); main.add(node); main.add(null); int sum = 0; while(main.size() > 0){ Node p = main.remove(); if(p == null){ lst.add(sum); main.add(null); sum = 0; p = main.remove(); if(p == null){ break; } } while(p != null){ sum += p.data; if(p.left != null){ main.add(p.left); } p = p.right; } } } }
@pradumanyadav38284 жыл бұрын
null point exception
@saketp185 жыл бұрын
I guess there is another way to do. Apply Hashmap where keys as distances and values as node data. Then add values and print the sum respective to key.
@gautamjangir8927 Жыл бұрын
*C++ Solution* vector diagonalSum(Node* root) { vectorans; queueq; q.push(root); q.push(NULL); int sum = 0; while(!q.empty()) { Node* temp = q.front(); q.pop(); if(temp==NULL) { q.push(NULL); temp = q.front(); q.pop(); if(temp==NULL) { break; } } while(temp!=NULL) { sum=sum+temp->data; if(temp->left) { q.push(temp->left); } temp = temp->right; } if(q.front()==NULL)//This is the small change that requires in the code for proper execution { ans.push_back(sum); sum = 0; } } return ans; }
@TechnicalBaniya7 жыл бұрын
I thing sir sum=0; statement should be in the the termination condition of the while loop because when p=dequeue(); statement will execute and then it is not NULL then sum will be initialized to zero.
@vivekanandkhyade7 жыл бұрын
inside the inner while , that is added again...so we don't miss that........but your observation is really good...Thanks.
@santhoshcts57 жыл бұрын
I see another issue , when it dequeues and it receives null , it breaks the while and makes sum = 0 , even though the diagonal is not fully completed . take example of 4,9 when 9 has both right and left as empty , it breaks the inner while loop and goes to outer while loop and gets 10 , but during this time , sum is assigned as 0 and the value will be lost, even though there are still few elements in diagonal to complete adding to the sum . the correct way should be initialize sum as 0 only after printing the sum ( which means end of current diagonal and moving to next diagonal )
@samcooper-025 жыл бұрын
@@santhoshcts5 i agree
@shivsagarshah243 жыл бұрын
@@santhoshcts5 correct
@shivsagarshah243 жыл бұрын
@@vivekanandkhyade it should be written(sum = 0) after you have printed sum.
@manassharma71925 жыл бұрын
Sir ,sum=0 should be in the if condition after printing the sum.
@shivsagarshah243 жыл бұрын
Yes, you are right!!!
@adityaranjan46396 жыл бұрын
sir, your algorithm is good but there is some mistake at condition of sum where you initialize sum ,so it isn't giving proper output. please check once again. if we initialize sum=0 at declaration time and in the condition where we print sum. , my code is : if(p==NULL) { cout
@rockyraj95714 жыл бұрын
awesome... sri ji
@nancygoel34734 жыл бұрын
this code needs to be optimized
@the-gray-haired-developer6 жыл бұрын
Appreciate your teaching techniques. For diagonal distance 3, you missed 15.
@ankushroy9194 жыл бұрын
is it necessary to put null in queue we just check if root.left is null then push and traverse right
@anshulasati13655 жыл бұрын
if we dqueue 6 then sum is 0,so its calculate initially..please describe
@DhananjayTyagi245 жыл бұрын
Amazing explanation sirji
@veerrajuyeleti85418 жыл бұрын
sir can you upload a video on find the node in a binary tree having equal subtrees on right and left
@veerrajuyeleti85418 жыл бұрын
sir cAN WE INCLUDE HASH TABLE FOR STORING THE SAME DIAGONAL VALUES