I can't believe myself that i done this problem without watching this video . Because it's my first tree problem ,I just saw GFG how to traverse tree and came up with solution which passed all testcase in single run with time complexity O(N) class Solution { public: int sum=0; void update(TreeNode* node){ if(node==nullptr){ return; } update(node->right); sum+=node->val; node->val=sum; update(node->left); } TreeNode* bstToGst(TreeNode* root) { update(root); return root; } };
thanks aryan GOAT
my dumbass thought vertical order traversal to get the sum of all the right nodes
I can't believe myself that i done this problem without watching this video . Because it's my first tree problem ,I just saw GFG how to traverse tree and came up with solution which passed all testcase in single run
with time complexity O(N)
class Solution {
public:
int sum=0;
void update(TreeNode* node){
if(node==nullptr){
return;
}
update(node->right);
sum+=node->val;
node->val=sum;
update(node->left);
}
TreeNode* bstToGst(TreeNode* root) {
update(root);
return root;
}
};
nice
u must be a genius
@@insaneclutchesyt948 but didn't optimal approach
Thanks
clear explanation
I didn't do any tree question till now should i do this ?
❤
can you please explain how recursive is using O(n) space , in itertative it's because of stack
Function stack space takes O(n)
recursive function calls are managed using call stack.
@@ajayprabhu465 👍
@@ITACH1688 👍
waiting for today's dc in place modification approach 🥲