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