LeetCode 257. Binary Tree Paths (Algorithm Explained)

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

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.

boredasever
Автор

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.

rohan
Автор

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.

michaellieberman
Автор

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

raghuramanswaminathan
Автор

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<String> binaryTreePaths(TreeNode root) {
StringBuilder strb= new StringBuilder();
List<String> list = new ArrayList<String>();
if(root==null)return list;

strb.append(""+root.val);
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<String> list) {
strb.append("->"+root.val);

list.add(strb.toString());
return ;
}
if(root.left!=null) trav(root.left, strb, list);
if(root.right!=null) trav(root.right, strb, list);
}*/

zi
Автор

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

tubex
Автор

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

soviaagustina
Автор

too much duplication, the smell of bad code.

davidlee