LeetCode 257. Binary Tree Paths (Algorithm Explained)

Ғылым және технология

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
AlgoCademy - algocademy.com/?referral=nick...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
Follow My Twitter - / nicholaswwhite
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nickwwhite?locale.x...
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Пікірлер: 24

  • @michaellieberman114
    @michaellieberman1144 жыл бұрын

    Forgive me for pointing out a trivial part of this problem, but the tree structure itself appears unrealistc... in what order were the values placed to put the 5 where it is? 1 was obviously placed first, then the 2 would have been placed to the right so the 3 had to have been placed 2nd. The 2 would have been the left child of 3 and the 5 would be the right child of 3... It just threw me off that the values didn't make sense, unless sorting algorithms had been used to arrange them like that. In terms of the algorithm itself, the one part I don't get is when you "return" on line 27, where are you returning to? Say there was a left and right child to 3, we go to left, place it on the current string and add it to result, then we should return to 3 to place the right child in its own path (the two paths would be "1->3->left" and "1->3->right") but I'm confused about how the algorithm does that. Ok I thoroughly reviewed the code and realized that "return" on line 27 goes back to line 30 (if we had entered that), "result" has changed, but "current_path" has not.

  • @rohan8758
    @rohan87583 ай бұрын

    Nice explanation through code walkthrough! But try to explian algorithm first & then codewalkthrough there might be some problem where walk through code explanation is best suited.

  • @irynasherepot9882
    @irynasherepot98824 жыл бұрын

    Thank you Nick!

  • @boredasever6645
    @boredasever66454 жыл бұрын

    How much time should I spend on my problems before I look at the solution ? I find my self looking at a solution, knowing how to do a problem for a while but then some time goes by and I get very unsure on the problem. What should I do to improve this.

  • @kasyapdharanikota8570

    @kasyapdharanikota8570

    2 жыл бұрын

    please help if you get any answer regarding this I am facing the same issue

  • @shiva4098

    @shiva4098

    2 жыл бұрын

    Spend 1 hour to solve the problem, if you're unable to solve it just take a break and come back then try again and repeat it at least for the entire day even though you are still stuck then watch the solution.

  • @kaushikramabhotla4635
    @kaushikramabhotla46353 жыл бұрын

    That keyboard sounds

  • @raghuramanswaminathan5187
    @raghuramanswaminathan51874 жыл бұрын

    Hey Nick, a small doubt.Why are we adding the result.add() in the main function when we are already adding the result.add() in the base case of our dfs? thanks

  • @TheSexGod

    @TheSexGod

    4 жыл бұрын

    Sometimes the main function never reaches the dfs function. In that case, we need to have the result.add() in the main function, specifically for the case when the root node has no children.

  • @melk48111
    @melk481118 ай бұрын

    well explained Nick

  • @zi6919
    @zi69194 жыл бұрын

    I tried the exact same method , only I used StringBuilder but i got wrong answers. If there is anything you can say please? /*public List binaryTreePaths(TreeNode root) { StringBuilder strb= new StringBuilder(); List list = new ArrayList(); if(root==null)return list; strb.append(""+root.val); if(root.left==null&&root.right==null){list.add(strb.toString());return list;} if(root.left!=null)trav(root.left,strb,list); if(root.right!=null)trav(root.right,strb,list); return list; } public void trav(TreeNode root,StringBuilder strb,List list) { strb.append("->"+root.val); if(root.left==null&&root.right==null){ list.add(strb.toString()); return ; } if(root.left!=null) trav(root.left,strb,list); if(root.right!=null) trav(root.right,strb,list); }*/

  • @maratbeiner7253

    @maratbeiner7253

    4 жыл бұрын

    That's because StringBuilder/Buffer concatenate values to the same instance of an object so you probably have same middle nodes (2 and 3 in this case) in both sides, and Strings are immutable so, you always create a new object that contains the values you actually concatenate

  • @tubex1300
    @tubex13004 жыл бұрын

    Hi Nick Will you make videos in leetcode using swift ? Thanks in advance

  • @NickWhite

    @NickWhite

    4 жыл бұрын

    nah sorry dude!

  • @tubex1300

    @tubex1300

    4 жыл бұрын

    Thanks

  • @rupaldesai7098
    @rupaldesai70984 жыл бұрын

    What is time complexity?

  • @Ace-nz2pd

    @Ace-nz2pd

    4 жыл бұрын

    O(n) -> n representing number of nodes in tree

  • @AleksandarIvanovicTV

    @AleksandarIvanovicTV

    3 жыл бұрын

    Every time he does appending to current string, JVM creates a new string and copies the content of previous, which takes O(n) times where n is the length of string, and he does that for every node in the tree, so it's O(n*m) where n is length of string and m is number of nodes. He should've used StringBuffer

  • @willturner3440
    @willturner34403 жыл бұрын

    I saw you in a movie 🤨🤨

  • @ahmedfattah5879
    @ahmedfattah58794 жыл бұрын

    11

  • @anvikimasti7982
    @anvikimasti798211 ай бұрын

    whole video is blurred.

  • @JohnWickea

    @JohnWickea

    4 ай бұрын

    time to change your glasses xD

  • @soviaagustina3434
    @soviaagustina34344 жыл бұрын

    Please make videos in leetcode using c, Thank you :)

  • @davidlee588
    @davidlee588 Жыл бұрын

    too much duplication, the smell of bad code.

Келесі