Convert a Binary Tree into Doubly Linked List | Love Babbar DSA Sheet | Amazon | Microsoft 🔥 | GFG
Вставка
- Опубліковано 8 лют 2025
- #tree #competitiveprogramming #coding #dsa
Hey Guys in this video I have explained with code how we can solve the problem 'Convert a Binary Tree into Doubly Linked List'.
Array question Playlist = • Love Babbar DSA 450 Qu...
String question Playlist = • Love Babbar DSA 450 Qu...
Searching and Sorting question Playlist = • Love Babbar DSA 450 Qu...
Binary Tree question Playlist = • Love Babbar DSA 450 Qu...
Dynamic Programming question Playlist = • Love Babbar DSA 450 Qu...
Roadmap for Dynamic Programming = • Complete Roadmap for D...
Great Strategy to solve DSA = • Great Strategy to solv...
My Journey to 5 star at codechef = • My Journey to 5 Star a...
Love Babbar DSA Sheet : drive.google.c...
Follow us on Instagram:
Shailesh Yogendra : / shaileshyogendra
Yogesh Yogendra : / i_am_yogesh_here
Follow us on LinkedIn:
Yogesh Yogendra : / yogesh-yogendra-26bbb518a
Shailesh Yogendra : / shailesh-yogendra-8b13...
Hope you like it. Comment if you have any doubt
LIKE | SHARE | SUBSCRIBE
instead of cheking (f==0) for the first node, we can check (prev==null) becoz prev will only be null in case of first node. No need of taking f variable.
we just want it enter if loop once without f it will enter for every node->left==null
You are right ,here is the code
void Solve(Node *root,Node *&head, Node*&prev)
{ if(root == nullptr) return;
Solve(root->left,head,prev);
if(!head) head = prev = root;
else{prev->right = root;
root->left = prev;
prev = root; }
Solve(root->right,head,prev);
}
Node * bToDLL(Node *root)
{ Node *head = NULL; Node *prev=NULL;
Solve(root,head,prev);
return head;
}
binary tree from strings....solve that question!!
All these videos shows how hard he worked to Crack a company like Amazon
Shailiesh and Yogesh, I wish you both all the success in the world and all the happniness for you and your family. God bless you.
Really superb explanation, Thank you!!
how we have connected last node right with head?
what an amazing question!!!!
Nice explanation.
clear and crisp solution
mujhe aaj pata laga ki shailesh brother and yogesh brother twins hai.
My code in java
class Solution
{
static void solve(Node root,Node head,Node prev){
if(root==null)return;
solve(root.left,head,prev);
if(prev==null){
head=root;
prev=root;
}
else{
prev.right=root;
prev.right.left=prev;
prev=prev.right;
}
solve(root.right,head,prev);
}
//Function to convert binary tree to doubly linked list and return it.
Node bToDLL(Node root)
{
// Your code here
Node head=null;
Node prev=null;
//int f=0;
solve(root,head,prev);
return head;
}
}
@@risewithsakshi Mera bhi nai aya hai
class Solution
{
static Node head;
static Node prev;
static void solve(Node root){
if(root==null)return;
solve(root.left);
if(prev==null){
head=root;
prev=root;
}
else{
prev.right=root;
prev.right.left=prev;
prev=root;
}
solve(root.right);
}
//Function to convert binary tree to doubly linked list and return it.
Node bToDLL(Node root)
{
// Your code here
head=null;
prev=null;
//int f=0;
solve(root);
return head;
}
}
aree java mein run hi nai hora gfg ka bhi erro dera hai,sab test casse solve nai hora
can we solve it iterartively?
Finally 🤩🤩🤩. Jaldi jaldi Binary Trees and Bst upload kar digiye 🥰🥰
This code not works in Java
ek graphic tablet lele code likhne ke liye paint main, 8000 ke aas pass aata hai wacom bamboo ka
thank
simplest solution using inorder
void insert(Node* & root, Node * & node){
node->right = root;
root->left = node;
node = root;
}
void inorder(Node* root, Node* &node, Node * pre){
if(!root)return;
inorder(root->left,node,pre);
insert(root,node);
inorder(root->right,node,pre);
}
Here you have used pass by ref ,so don't need to take f extra variable for checking, initially head is null but after that it won't
This works
class Solution
{
public:
Node* head = NULL;
Node* prev = NULL;
void solve(Node* root){
if(root == NULL)
return;
solve(root->left);
if(prev==NULL){
head = root;
prev = root;
}
else{
prev->right = root;
prev->right->left = prev;
prev = root;
}
solve(root->right);
}
//Function to convert binary tree to doubly linked list and return it.
Node * bToDLL(Node *root)
{
solve(root);
return head;
}
};
bro code is not working , segmentation error aa rha he
Nice explanation!
Can someone explain or send code if we want to print ''Preorder' or 'Postorder traversal'?
Preorder(Node* root){
coutdata);
Preorder(root->left);
Preorder(root->right);
}
Postorder(Node* root){
Postorder(root->left);
Postorder(root->right);
coutdata;
}
gfg ka set 3 ka code 100 percent accept hora hai set 2 ka error dera hai aur yeh codelibrary ka code jab java mein likha to koi output hi nai aya
+1
instead of prev->right->left= prev we can also write root->left= prev ??
but we have to return the head in the end so , we cant update the head like that
@@tusharshaily I am not talking about updating the head, I am saying that root->left= prev that is the same as prev->right->left= prev
Bro I also did the same... It works fine...
yes you can
🔥 🔥
concept samajh aagay but code ka output nai aya//c++ mein segmentation error
Can someone explain the use of call by reference for "prev" ?
Bro in question ko lgakar coding round clear ho jayega kyu
Yaa
But don't try to learn this questions... try to understand the concept behind the questions
@@CodeLibrary yes 👍
yaar bhai code toh description mein daal diya kar, copy paste karne mein aasame rahegi
Don't copy paste, write by your own even if by seeing it.
answer coming wrong if reference is not passed with f and also with prev, can anybody explain??
I guess it is due to some stack issues (the stack of function calls). I guess there is some stack corruption in play here. Don't worry about that. You can easily get over this issue by making head and prev pointers global/ (or in gfg you can declare those as a member variable for the "Solution" class (public or private doesn't matter). You must although initialize them with the value "nullptr" before calling the recursive function. Also take a note that now you don't need to pass the head prev pointers by reference now. You only need to pass root and f.
Please upload the video of bst
Understand
Expected auxillary space should be height of tree and in this problem video it would be no of nodes in tree .
Done!
c++ mein jab yeh code run kiya to segmentation error dera hai,ajeeb chutiyaap hora hai gfg mein
same bro har question me error deta h gfg
Bhai reading n kia kro logic intuit ni kr skte to likh kr pdha n kro
bhai explain thoda kam kiya hai ques banaya hai aapne ,
ekdum hi noob hai fir tu sab samajh aa gya mujhe to itna badiya explain kiya hai
bro 2-3 din mai binary tree khtm kar du..
Easy C++ solution, solution by inorder traversal
class Solution
{
public:
Node* inorder(Node *root){
if(root->left == NULL && root->right == NULL) {
return root;
}
Node *temp1 = NULL;
if(root->left) temp1 = inorder(root->left);
Node *temp2 = NULL;
if(root->right) temp2 = inorder(root->right);
root->right = temp2;
if(temp2){
temp2->left = root;
}
if(temp1){
Node *tail = temp1;
while(temp1->right != NULL){
temp1 = temp1->right;
}
temp1->right = root;
root->left = temp1;
return tail;
}
else{
root->left = NULL;
return root;
}
}
Node * bToDLL(Node *root)
{
// your code here
root = inorder(root);
return root;
}
};
Bhai tum na hote to kya hota
Tone is so boring i got some sleep
haan toh sojaa placement koi aur le jaayega
@@chiragtejwani1803 haha