1038. Binary Search Tree to Greater Sum Tree | Recursive | Iterative | Morris Traversal

Поделиться
HTML-код
  • Опубликовано: 30 сен 2024

Комментарии • 15

  • @youtubeuserlovesyoutube2207
    @youtubeuserlovesyoutube2207 3 месяца назад +5

    thanks aryan GOAT

  • @insaneclutchesyt948
    @insaneclutchesyt948 3 месяца назад

    my dumbass thought vertical order traversal to get the sum of all the right nodes

  • @codebreaker6578
    @codebreaker6578 3 месяца назад +11

    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;
    }
    };

  • @mohammedansil2302
    @mohammedansil2302 3 месяца назад

    Thanks
    clear explanation

  • @codebreaker6578
    @codebreaker6578 3 месяца назад +7

    I didn't do any tree question till now should i do this ?

  • @cenacr007
    @cenacr007 3 месяца назад

  • @codebreaker6578
    @codebreaker6578 3 месяца назад

    can you please explain how recursive is using O(n) space , in itertative it's because of stack

    • @ajayprabhu465
      @ajayprabhu465 3 месяца назад +3

      Function stack space takes O(n)

    • @ITACH1688
      @ITACH1688 3 месяца назад +3

      recursive function calls are managed using call stack.

    • @codebreaker6578
      @codebreaker6578 3 месяца назад

      @@ajayprabhu465 👍

    • @codebreaker6578
      @codebreaker6578 3 месяца назад

      @@ITACH1688 👍

  • @lokesh8660
    @lokesh8660 3 месяца назад

    waiting for today's dc in place modification approach 🥲