Video-11 CORRECTION : We are visiting from head for every further nodes in tree to connect. So, Time will be O(n^2) but this can be reduced to O(n) if we keep track of the last node of the list we have formed. See the code below : /******** ************************C++ ********************************/ class Solution { public: Node* prev = NULL; Node *flattenBST(Node *root) { if(!root) { return NULL; } Node* head = flattenBST(root->left); //Trust root->left = NULL; if(prev) { prev->right = root; prev = root; } else { prev = root; } root->right = flattenBST(root->right); //Trust if(head == NULL) { return root; } return head; } }; /******************************** JAVA ********************************/ Class Solution { private Node prev = null; public Node flattenBST(Node root) { if (root == null) { return null; } Node head = flattenBST(root.left); // Trust root.left = null; if (prev != null) { prev.right = root; prev = root; } else { prev = root; } root.right = flattenBST(root.right); // Trust if (head == null) { return root; } return head; } }
Theres'a another variation of this qsn on Leetcode: Medium, Premium == 114. Flatten Binary Tree to Linked List (not BST but BT) Great video! Thank you :)
I love the way to build the intiution 💖 Here's some modification : T.C is actually O(n log n) not n^2 bcz at max the while loop will traverse to the max depth of BST which is log(N) .... :).............but i've solved it with O(N) time using 2 pointer and recursion here's the code : Node *flattenBST(Node *root) { return f(root,NULL); } Node * f(Node * root,Node * prev){ if(!root) return NULL; Node * left = f(root->left,root); Node * right = f(root->right,prev);
//User function Template for Java class Solution { public Node flattenBST(Node root) {
if(root==null) { return null; } if(root.left==null&&root.right==null) { return root; } Node head1=flattenBST(root.left); root.left=null; Node temp=head1; while(temp!=null && temp.right!=null) { temp=temp.right; } temp.right=root; root.right=flattenBST(root.right); return head1; } } @codestorywithMIK bro can you tell me why this code fails I wrote this i mean it should pass but I dont know why it fails please check
No problem bro I figured out WHEN ARE U BRINGING BACKTRACKING CONCEPTS PLEASE COVER THOSE MINUTE DETAILS i MENTIONED UNDER COMMENT SECTION OF ONE OF YOUR VIDEOS IF U DONT REMEMBER I CAN REMIND YOU AGAIN WHAT I AM TALKING ABOUT
Video-11
CORRECTION :
We are visiting from head for every further nodes in tree to connect. So, Time will be O(n^2) but this can be reduced to O(n) if we keep track of the last node of the list we have formed. See the code below :
/******** ************************C++ ********************************/
class Solution
{
public:
Node* prev = NULL;
Node *flattenBST(Node *root) {
if(!root) {
return NULL;
}
Node* head = flattenBST(root->left); //Trust
root->left = NULL;
if(prev) {
prev->right = root;
prev = root;
} else {
prev = root;
}
root->right = flattenBST(root->right); //Trust
if(head == NULL) {
return root;
}
return head;
}
};
/******************************** JAVA ********************************/
Class Solution {
private Node prev = null;
public Node flattenBST(Node root) {
if (root == null) {
return null;
}
Node head = flattenBST(root.left); // Trust
root.left = null;
if (prev != null) {
prev.right = root;
prev = root;
} else {
prev = root;
}
root.right = flattenBST(root.right); // Trust
if (head == null) {
return root;
}
return head;
}
}
can you explain how it will be O(n^2) for the solution discussed in the video.
Theres'a another variation of this qsn on Leetcode:
Medium, Premium == 114. Flatten Binary Tree to Linked List (not BST but BT)
Great video! Thank you :)
I love the way to build the intiution 💖 Here's some modification :
T.C is actually O(n log n) not n^2 bcz at max the while loop will traverse to the max depth of BST which is log(N) .... :).............but i've solved it with O(N) time using 2 pointer and recursion
here's the code :
Node *flattenBST(Node *root)
{
return f(root,NULL);
}
Node * f(Node * root,Node * prev){
if(!root) return NULL;
Node * left = f(root->left,root);
Node * right = f(root->right,prev);
if(prev and prev->left!=NULL){
root->right = prev;
prev->left=NULL;
if(!left) return root;
return left;
}
root->right = right;
if(!left) return root;
return left;
}
Thank you so much.
Yes you are correct, I have added the correction in the caption and also in the Pinned Comment of this video.
maximum depth of n hi hoga na ...so isnt it O(n2)
yeah n hi hoga depth but ye binary search tree hey na so max depth can be log(n) that's why it'll be o(nlog(n))
@Wadwa
cover this problem using morris traversal
bro you are on next level mtlb feel krra diya code
love the way you teach
Thank you for bringing the videos daily ❤️
Damn good mik!! I am your big fan❤
I now believe Recursion is indeed magic.
11/18 done [28.5.24] ✅✅
class Solution {
private Node prev;
private Node head;
public Node treeToDoublyList(Node root){
if(root==null) return null;
prev=null;
head=null;
solve(root);
prev.right=head;
head.left=prev;
return head;
}
private void solve(Node root){
if(root==null) return
private void solve(Node root){
if(root ==null) return;
solve(root.left)
if(prev!=null){
prev.right=root;
root.left =prev;
}else{
head=root;
}
prev=root;
solve(root.right);
}
}
🎉❤
Thanks a lot bhaiya ❤❤. Can you please make a video on Diameter of a Binary Tree, there are several videos out there but every video is confusing
yes yes , I will cover those too.
Also, please see my Pinned comment ❣
Can we use morris traversal to solve this question
Sir apse doubt kaise puch skte hai
hum first hai
Thank you 🙂
Also, please see my Pinned comment ❣
Thanks a lot mik
//User function Template for Java
class Solution {
public Node flattenBST(Node root) {
if(root==null)
{
return null;
}
if(root.left==null&&root.right==null)
{
return root;
}
Node head1=flattenBST(root.left);
root.left=null;
Node temp=head1;
while(temp!=null && temp.right!=null)
{
temp=temp.right;
}
temp.right=root;
root.right=flattenBST(root.right);
return head1;
}
}
@codestorywithMIK bro can you tell me why this code fails I wrote this i mean it should pass but I dont know why it fails please check
No problem bro I figured out WHEN ARE U BRINGING BACKTRACKING CONCEPTS PLEASE COVER THOSE MINUTE DETAILS i MENTIONED UNDER COMMENT SECTION OF ONE OF YOUR VIDEOS IF U DONT REMEMBER I CAN REMIND YOU AGAIN WHAT I AM TALKING ABOUT
This above code idea is not suitable for solving bst flatten in leetcode