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; } };
@abhinay.k5 ай бұрын
nice
@insaneclutchesyt9485 ай бұрын
u must be a genius
@codebreaker65785 ай бұрын
@@insaneclutchesyt948 but didn't optimal approach
@cenacr0075 ай бұрын
❤
@codebreaker65785 ай бұрын
I didn't do any tree question till now should i do this ?
@insaneclutchesyt9485 ай бұрын
my dumbass thought vertical order traversal to get the sum of all the right nodes
@codebreaker65785 ай бұрын
can you please explain how recursive is using O(n) space , in itertative it's because of stack
@ajayprabhu4655 ай бұрын
Function stack space takes O(n)
@ITACH16885 ай бұрын
recursive function calls are managed using call stack.
@codebreaker65785 ай бұрын
@@ajayprabhu465 👍
@codebreaker65785 ай бұрын
@@ITACH1688 👍
@lokesh86605 ай бұрын
waiting for today's dc in place modification approach 🥲