No video

Populating Next Right Pointers in Each Node | Tree Data Structure playlist C++ | Hello World

  Рет қаралды 7,587

Hello World

Hello World

Күн бұрын

This is the video under the series of DATA STRUCTURE & ALGORITHM in a TREE Playlist. We are going to understand How to Populating Next Right Pointers in Each Node.
Join My Telegram channel for more Updates: telegram.me/he...
complete DSA preparation: github.com/Pri...
► 116. Populating Next Right Pointers in Each Node
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
We also Provide courses on Competitive Programming and Data structure and Algorithms. Please see our Full Playlist on our Channel.
----------------------------------------------------------------------------------------:
Populating Next Right Pointers in Each Node: leetcode.com/p...
code in This Video: github.com/Pri...
----------------------------------------------------------------------------------------
** Unacademy
Use my code "HELLOWORLD" to get 10% discount
Career as a Developer: unacademy.com/...
Tech Extracurriculars: bit.ly/Unacade...
----------------------------------------------------------------------------------------
*Follow me *
LinkedIn► / iamprince
Facebook► / helloworldofficials
Instagram► / helloworldbyprince
Twitter► / prince_king_
Telegram► telegram.me/he...
----------------------------------------------------------------------------------------
►Our Playlists on:-
► Tree: • Tree Data Structure & ...
► Hashing: • Hashing Data Structure...
► Matrix: • Matrix (Multidimension...
► STL: • Standard Template Libr...
► Leetcode: • LeetCode Solutions And...
►Competitive Programming: • Full course in Competi...
►C++ Full Course : • C++ full Course in HINDI
►Algorithms: • L-01 || Prefix Sum Arr...
►Data Structure: • Data Structures with C...
------------------------------------------------------------------------
🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
✨ Tags ✨
Populating Next Right Pointers in Each Node
perfect binary tree
The tree Data structure in Hindi
Find the Invert of a Binary Tree
Use of Tree Data structure in real Life
question asked in Google
off-campus placement
how to learn to code for beginners
Practice Tree data structure
tree in data structure
Best Telegram channel for Off-campus Placement drive
Tree in a data structure in Hindi
Tree Full playlist for Beginners
#Tree #Leetcode #programming

Пікірлер: 25
@girikgarg8
@girikgarg8 Жыл бұрын
There's a follow-up which asks us to do this question using constant extra space (recursive stack space is not considered as extra space). How would you do it then?
@surjeetsingh433
@surjeetsingh433 3 жыл бұрын
Keep growing brother 💙
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
We will
@shaantyagi2187
@shaantyagi2187 2 жыл бұрын
Aa gya samajh bhaiya, time laga magar aa gya ...ek baar fir se level order traversal revise kiya tab samajh gye!
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
bass aise hi revision karte rho samjh me aa jayega chiz
@ujjwalsharmaiiitunacse3884
@ujjwalsharmaiiitunacse3884 6 ай бұрын
Great content sir !! keep up the good work
@vanivaishnavi3916
@vanivaishnavi3916 3 жыл бұрын
Well well...you arr best
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
thanks a lot vani
@RYFINTECH
@RYFINTECH 2 жыл бұрын
C++ || Easy to understand solution class Solution { public: Node* connect(Node* root) { if(root == NULL) return root; queue q; q.push(root); while(!q.empty()){ int size = q.size(); for(int i=0;inext = q.front(); // last element na ho, 0 based h if(temp ->left != NULL) q.push(temp->left); if(temp->right != NULL) q.push(temp->right); } } return root; } };
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Amazing yaar ✅
@thegreekgoat98
@thegreekgoat98 2 жыл бұрын
I DID THIS USING SIMPLE DFS:: Node* connect(Node* root) { Node* black=root; while(black!=NULL && black->left!=NULL) { Node* n=black; while(true) { n->left->next=n->right; if(n->next==NULL) break; n->right->next=n->next->left; n=n->next; } black=black->left; } return root; }
@MANISHKUMAR-vn1yh
@MANISHKUMAR-vn1yh 2 жыл бұрын
MOJ krdi bhaiyya
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Yeah 😂😂
@harshsrivastava2075
@harshsrivastava2075 Жыл бұрын
class Solution { public: Node* connect(Node* root) { if (!root) { return root; } queue q; q.push(root); while (!q.empty()) { int sizee = q.size(); for (int i = 0; i < sizee; i++) { Node* current = q.front(); q.pop(); if (i != sizee - 1) { current->next = q.front(); } if (current->left) { q.push(current->left); } if (current->right) { q.push(current->right); } } } return root; } }; more optimized solution. Wrote it without seeing the solution!
@gamerversez5372
@gamerversez5372 Жыл бұрын
My Code in C++ Node* connect(Node* root) { if(!root) return NULL; queue q; q.push(root); while(!q.empty()) { int size = q.size(); vector x; while(size!=0) { if(size==0) break; else if(size==1) { auto i=q.front(); q.pop(); if(i->left) q.push(i->left); if(i->right) q.push(i->right); break; } else if(size>1) { auto i=q.front(); q.pop(); if(i->left) q.push(i->left); if(i->right) q.push(i->right); auto nex = q.front(); i->next=nex; size-=1; } } } return root; }
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
good work
@shubhambhatt2704
@shubhambhatt2704 Жыл бұрын
Why is this question marked medium though it is easy?
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
idk
@pritishpattnaik4674
@pritishpattnaik4674 2 жыл бұрын
I guess we can use level order traversal here
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
yo
@cr7johnChan
@cr7johnChan 2 жыл бұрын
if(root==NULL)return root; queueq; q.push(root); Node*temp; while(q.empty()!=1){ int s =q.size(); while(s){ temp=q.front(); q.pop(); if(s==1)temp->next==NULL; else{ temp->next=q.front(); } if(temp->left!=NULL)q.push(temp->left); if(temp->right!=NULL)q.push(temp->right); s--; } } return root; }
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Ekdum jhakaas
@harshalgarg1149
@harshalgarg1149 2 жыл бұрын
This solution is better. Check this out. Node* connect(Node* root) { if(root==NULL) return root; queue q; q.push(root); while(!q.empty()) { int size = q.size(); while(size--) { Node* top = q.front(); q.pop(); if(top->left) q.push(top->left); if(top->right) q.push(top->right); if(size!=0) top->next = q.front(); } } return root; }
I Took a LUNCHBAR OFF A Poster 🤯 #shorts
00:17
Wian
Рет қаралды 15 МЛН
GTA 5 vs GTA San Andreas Doctors🥼🚑
00:57
Xzit Thamer
Рет қаралды 25 МЛН
L38. Flatten a Binary Tree to Linked List | 3 Approaches | C++ | Java
21:51
Google Coding Interview With A Facebook Software Engineer
49:59
Clément Mihailescu
Рет қаралды 934 М.