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