Convert BST to Greater Tree - Leetcode 538 - Python

Поделиться
HTML-код
  • Опубликовано: 12 июн 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🥷 Discord: / discord
    🐦 Twitter: / neetcode1
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    Problem Link: leetcode.com/problems/convert...
    0:00 - Read the problem
    1:05 - Drawing Explanation
    5:34 - Coding Explanation
    leetcode 538
    #amazon #interview #python
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • НаукаНаука

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

  • @dustinscroggins6256
    @dustinscroggins6256 2 года назад +10

    Hey man I just want to say thanks for all you do for the community. I landed my dream job at Microsoft and your channel was a big part of me building up my DSA foundations. Everybody out there keep practicing these problems I swear it'll pay off in the long run

  • @Anon-xz6hu
    @Anon-xz6hu 2 года назад +27

    Hey, I've a request can you make new series on minimal dsa concepts (required for solving problems and understanding concepts) using python.
    Guys like this comment if you want it too(it'll appear on top and he'll read it hopefully 😅)

  • @numberonep5404
    @numberonep5404 2 года назад +10

    It took me waaay too long to find the solution but for what it is worth, you don't need a tmp variable if you do an assignment:
    def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
    prev = 0
    def dfs(node):
    nonlocal prev
    if node:
    dfs(node.right,)
    node.val += prev
    prev= node.val
    dfs(node.left)
    dfs(root)
    return root

  • @curimeowcat6957
    @curimeowcat6957 2 года назад +1

    Thanks! Very awesome videos! If you have google code jam tutorial, not just for leetcode, I would definitely join the member channel!

  • @numberonep5404
    @numberonep5404 2 года назад +7

    Nice explanation as always! Do you mind doing "307. Range Sum Query - Mutable" or "37. Sudoku Solver" or something about A* someday? It will force me to do them :S i mean you already managed to make me do KMP and the daily leetcode challenges, what is a few more good deeds?

  • @abheykalia3409
    @abheykalia3409 2 года назад +17

    You could just use : curSum+=node.val
    node.val=curSum

  • @madhumithakolkar_
    @madhumithakolkar_ 7 месяцев назад

    Great explanation as always. You don't really need to mention curSum as nonlocal or have a tmp variable actually:
    class Solution:
    def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
    curSum = [0]
    def dfs(root):
    if not root : return
    dfs(root.right)
    root.val += curSum[0]
    curSum[0] = root.val
    dfs(root.left)
    dfs(root)
    return root

  • @dossymzhankudaibergenov8193
    @dossymzhankudaibergenov8193 2 года назад

    Great expleniation, and which app do you use to drawing?

  • @Od253
    @Od253 2 года назад

    God damn so easy but just tricky enough. Thanks for the vid.

  • @jarvispotter6125
    @jarvispotter6125 2 года назад +1

    Hi Neetcode, thanks for your amazing explanations. cannot think of solving DSA with out your videos. thanks for making it possible for me. i have a request. is it possible for u to create playlists based on companies ?

  • @jessanraj9086
    @jessanraj9086 2 года назад

    Really nice

  • @Mukesh-nx8tf
    @Mukesh-nx8tf 2 года назад

    what app do you use to record screen and for white board?

  • @123souparno
    @123souparno Год назад +1

    We can also Pass the curSum as a member variable in the Constructor initialize part. Then inside DFS we don't need to mention curSum as global variable.

  • @selfishmango5116
    @selfishmango5116 2 года назад

    Yo which language do you recommend for competitive programming because everyone says pythons not good for that

    • @akhilnarayanan7182
      @akhilnarayanan7182 2 года назад +2

      The opposite happened to me, I have my work experience in Python, but when the interviewer gave me questions to solve, I chose C++
      Then he asked, why you choose C++ when you have X years of experience in Python

  • @ilirhajrullahu4083
    @ilirhajrullahu4083 2 года назад +1

    Hello Neetcode, thank you for these great videos and your explanation. Could you please share with us what drawing tablet do you use and what software do you use for video editing? Thank you and I am eagerly waiting for your new videos!

  • @zungulutrungu6407
    @zungulutrungu6407 2 года назад

    King.

  • @avichai987789
    @avichai987789 2 года назад

    hey there NeetCode, love your channel its really bright and understandable!
    can you make a video on:
    1202. Smallest String With Swaps?
    there are coupe of videos on youtube but i didnt quite understand their explanations.

  • @danielsun716
    @danielsun716 Год назад

    If you want, we could use a list to store the curSum, and modify it in the nested function, Cause it is a shallow copy. And no need to store the tmp.
    def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
    curSum = [0]
    def dfs(node):
    if not node:
    return None
    dfs(node.right)
    '''
    tmp = node.val
    node.val += curSum[0]
    curSum[0] += tmp
    '''
    node.val += curSum[0]
    curSum[0] = node.val
    dfs(node.left)
    dfs(root)
    return root

  • @pushkarchaubey8668
    @pushkarchaubey8668 2 года назад

    Can we make inorder traversal in original tree and stored in an array
    After that with prifix logic sum we make another array in sorted manner
    And with logic of makin bst with arr we can return new root

  • @brajagopalmukherjee1588
    @brajagopalmukherjee1588 2 года назад

    Sir need an explanation of Word break problem on lc

  • @cmdv42
    @cmdv42 2 года назад

    💯✨

  • @friction5001
    @friction5001 2 года назад

    Let’s gooo

  • @johnniewalkerjohnniewalker2459
    @johnniewalkerjohnniewalker2459 2 года назад

    you can also solve this problem doing first inorder traversal and using after the technique of prefix sum .Here is my solution in c++
    class Solution {
    public:
    void dfs_inorder(TreeNode* root, vector &v, vector &ans){
    if (root != NULL){
    dfs_inorder(root->left, v, ans);
    ans.push_back(root);
    v.push_back(root->val);
    dfs_inorder(root->right, v, ans);
    }
    }
    TreeNode* convertBST(TreeNode* root) {
    if (root == NULL)
    return NULL;
    vector ans;
    vector v;
    dfs_inorder(root, v, ans);
    for (int i=1;ival = v[n-1];
    for (int i=1;ival = (v[n-1]-v[i-1]);
    }
    return root;
    }
    };

  • @wewe-fx6un
    @wewe-fx6un 2 года назад +1

    Postorder?

    • @jeremyliang7159
      @jeremyliang7159 2 года назад +7

      Nope. Postorder is left first, then right, and finally the middle node. This is actually reversed inorder traversal, which is right first, then middle node, and finally left.

    • @wewe-fx6un
      @wewe-fx6un 2 года назад

      @@jeremyliang7159 got it

  • @kaustubhwaghavkar9079
    @kaustubhwaghavkar9079 Год назад

    Hey Neetcode, you could also eliminate temp by doing something like this(just carry the updated root.val as currSum for further iterations): Just a suggestion :). BTW, great work!
    dfs(root.right)
    root.val += currSum
    currSum = root.val

  • @mohaimenchowdhury
    @mohaimenchowdhury 10 месяцев назад

    The main catch of this problem is the part is updating the curSum 😴

  • @asdfasyakitori8514
    @asdfasyakitori8514 2 года назад

    I can't solve this😂

  • @luanlucas8605
    @luanlucas8605 2 года назад

    Thanks a lot for another video and amazing explanation. I'd like to ask you a favor though (or maybe a suggestion): it's often hard for me to see the key takeaway from the problem that would make me (us) go in the right direction for the solution, so something that would definitely help me (and possibly others) is to highlight (perhaps written in red with big font? :D) the key words we should keep in mind from your explanation. I usually struggle to find the right topic that would give me the optimal solution (should I use a hash map? a tree? only array manipulation? is it graph?) and then once I figure it out, I still have a hard time building up the solution idea.
    I don't know if my description was clear enough (I hope so) but it doesn't matter, again, thanks a lot for your effort and videos (and recently your algo list at neetcode.io) and congrats for landing a job at Google.
    All the best

  • @sidazhong2019
    @sidazhong2019 6 месяцев назад

    Use self.curSum. I don't think tmp is necessary. U can do the calculation first "curSum+=node.val" then update the node "node.val=curSum". since the initial curSum is 0.
    self.max=0
    def dfs(root):
    if not root:
    return
    dfs(root.right)
    self.max+=root.val
    root.val=self.max
    dfs(root.left)
    dfs(root)
    return root