Diagonal Sum in Binary Tree (Algorithm)

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

Vivekanand Khyade - Algorithm Every Day

Vivekanand Khyade - Algorithm Every Day

Күн бұрын

Print the sum of every diagonal in binary tree.
Print diagonal elements: • Print diagonal element...

Пікірлер: 29
@anandkulkarni2111
@anandkulkarni2111 4 жыл бұрын
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.!!!
@vivekanandkhyade
@vivekanandkhyade 4 жыл бұрын
yess.... do u know someone who can help in making animations.. ?
@pradumanyadav3828
@pradumanyadav3828 4 жыл бұрын
null point exception
@pradumanyadav3828
@pradumanyadav3828 4 жыл бұрын
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; } } } }
@pradumanyadav3828
@pradumanyadav3828 4 жыл бұрын
null point exception
@saketp18
@saketp18 5 жыл бұрын
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
@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; }
@TechnicalBaniya
@TechnicalBaniya 7 жыл бұрын
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.
@vivekanandkhyade
@vivekanandkhyade 7 жыл бұрын
inside the inner while , that is added again...so we don't miss that........but your observation is really good...Thanks.
@santhoshcts5
@santhoshcts5 7 жыл бұрын
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-02
@samcooper-02 5 жыл бұрын
@@santhoshcts5 i agree
@shivsagarshah24
@shivsagarshah24 3 жыл бұрын
@@santhoshcts5 correct
@shivsagarshah24
@shivsagarshah24 3 жыл бұрын
@@vivekanandkhyade it should be written(sum = 0) after you have printed sum.
@manassharma7192
@manassharma7192 5 жыл бұрын
Sir ,sum=0 should be in the if condition after printing the sum.
@shivsagarshah24
@shivsagarshah24 3 жыл бұрын
Yes, you are right!!!
@adityaranjan4639
@adityaranjan4639 6 жыл бұрын
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
@rockyraj9571
@rockyraj9571 4 жыл бұрын
awesome... sri ji
@nancygoel3473
@nancygoel3473 4 жыл бұрын
this code needs to be optimized
@the-gray-haired-developer
@the-gray-haired-developer 6 жыл бұрын
Appreciate your teaching techniques. For diagonal distance 3, you missed 15.
@ankushroy919
@ankushroy919 4 жыл бұрын
is it necessary to put null in queue we just check if root.left is null then push and traverse right
@anshulasati1365
@anshulasati1365 5 жыл бұрын
if we dqueue 6 then sum is 0,so its calculate initially..please describe
@DhananjayTyagi24
@DhananjayTyagi24 5 жыл бұрын
Amazing explanation sirji
@veerrajuyeleti8541
@veerrajuyeleti8541 8 жыл бұрын
sir can you upload a video on find the node in a binary tree having equal subtrees on right and left
@veerrajuyeleti8541
@veerrajuyeleti8541 8 жыл бұрын
sir cAN WE INCLUDE HASH TABLE FOR STORING THE SAME DIAGONAL VALUES
@vivekanandkhyade
@vivekanandkhyade 8 жыл бұрын
yes you can.
@veerrajuyeleti8541
@veerrajuyeleti8541 8 жыл бұрын
well understood
Side view of binary tree Algorithm(Left and right side view)
4:34
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 30 М.
Print diagonal elements in binary tree(code/Algorithm/Program)
25:19
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 20 М.
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
Spiral (zig-zag) level order traversal of a binary tree
14:12
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 34 М.
Diameter of a Binary Tree (Code/ Algorithm)
17:15
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 95 М.
Bottom view of a Binary Tree Algorithm
12:28
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 36 М.
I Spent 100 Hours Inside The Pyramids!
21:43
MrBeast
Рет қаралды 67 МЛН
How to Remember Everything You Read
26:12
Justin Sung
Рет қаралды 4,3 МЛН
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 344 М.
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Level Order Traversal of a Binary Tree (level by level and as a whole)
13:54
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 51 М.
L21. Vertical Order Traversal of Binary Tree | C++ | Java
18:53
take U forward
Рет қаралды 362 М.