LeetCode N-ary Tree Postorder Traversal Solution Explained - Java

  Рет қаралды 50,241

Nick White

Nick White

Күн бұрын

Пікірлер: 49
@themingband
@themingband 4 жыл бұрын
Dude you gotta manage your mic clipping the output. Great content, but hard to listen to.
@ConwellConwell
@ConwellConwell Жыл бұрын
Nick helped code 911 and grinder
@milad950
@milad950 5 жыл бұрын
dude massively appreciate these videos!
@antonantochi8498
@antonantochi8498 2 жыл бұрын
Classic recursive DFS here...what is stack for? When you starting to use linkedlist as a stack, always think first if you really on a correct way :)
@simonkaranja3811
@simonkaranja3811 2 жыл бұрын
Thank you my code teacher for always doing an amazing work. Look at this solution. Has a runtime of 1 ms. class Solution { List ans = new ArrayList(); public List postorder(Node root) { if(root == null) return ans; for(Node child : root.children){ postorder(child); } ans.add(root.val); return ans; } }
@sharidass1408
@sharidass1408 4 жыл бұрын
Good Job Nick. Helps me to wrap my head around problems quickly
@AnotherCogInTheMurderMachine
@AnotherCogInTheMurderMachine Жыл бұрын
Can somebody please explain why does the "for-each" loop not throwing a Null Pointer exception when "node.children" is null ?????? For example Node with val = 5 does not have nay children, but in this code we still try to iterate through his children
@HitHard1008
@HitHard1008 Жыл бұрын
The foreach loop will not even run once as node.children.length will be zero.
@Matlaps123
@Matlaps123 Жыл бұрын
Great video! Is there any specific reason you didnt just use a normal stack, Nick?
@awesomebearaudiobooks
@awesomebearaudiobooks Жыл бұрын
In Java, it's preferable to use LinkedList instead of Stack, because under the hood, Stack is implemented/inherited as a child of the Vector class, which is generally slower than using the LinkedList, which is a direct implementation of the List interface. In fact, it's recommended to never use anything that inherits from a Vector class, unless you REALLY need it. For example, Stack (or any other Vector) would be a viable option if you had a very old version of Java AND if your project requires multithreading (because in the older versions of Java, you couldn't use LinkedList in multithreaded apps, while you could use Stack). In the modern versions of Java, LinkedList is almost always preferable to Stack.
@syakov06
@syakov06 3 жыл бұрын
Why not to use recursion?
@mohammedghabyen721
@mohammedghabyen721 2 жыл бұрын
Another way to solve this in c#: public class Solution { public IList Postorder(Node root) { IList res = new List(); if(root==null) return res; if(root.children.Count==0) { res.Add(root.val); return res; } addData(root,res); return res; } private void addData(Node node,IList res){ for(int i=0;i
@Firecloak
@Firecloak 4 жыл бұрын
short and straightforward. thanks
@Turnpost2552
@Turnpost2552 3 жыл бұрын
Name of stack was bad naming. Its a friggen linkedlist. However it behaves like a stack.
@RBNrocks
@RBNrocks 11 ай бұрын
class Solution { public List postorder(Node root) { List result = new LinkedList(); process(root, result); return result; } private void process(Node root, List result) { if(root == null) { return; } for(Node child : root.children) { process(child, result); } result.add(root.val); } }
@javaluvawithjeremystones6315
@javaluvawithjeremystones6315 3 жыл бұрын
Thanks for making these vids. Stack is LIFO, yours is just a backwards queue, not a stack, because it’s a FIFO implementation.
@Sandeep-tx5jh
@Sandeep-tx5jh 3 жыл бұрын
In array, elements by default are inserted at last and he used polllast() it implies stack itself right
@albert_schneider554
@albert_schneider554 5 жыл бұрын
do the tech companies allow to use c++ stl and other utility functions during tech interview???
@xavierelon
@xavierelon 4 жыл бұрын
Yes of course
@treyquattro
@treyquattro 4 жыл бұрын
yes but you should know how to implement the algorithms you use
@joycesilvera8214
@joycesilvera8214 4 жыл бұрын
don't we need Collection.reverse(node.children)??
@spandiar
@spandiar 2 жыл бұрын
How is the "for" loop not throwing a Null Pointer exception when "node.children" is null?
@dineshkumar-kw1ik
@dineshkumar-kw1ik 2 жыл бұрын
have you got the answer?
@hannikyaw9106
@hannikyaw9106 4 жыл бұрын
you are complicating it a lot more. we can just use a recursive approach to populate the result. it is cleaner and faster too. And a stack is not necessary.
@sarthakhejib8290
@sarthakhejib8290 4 жыл бұрын
Yes, by using recursion the solution would be of 3 lines xd, but one should emphasis more on iterative approach rather than the recursive one. :)
@joycesilvera8214
@joycesilvera8214 4 жыл бұрын
but this is a follow up question asked on the recursive approach, being prepared doesn't harm!
@geoffreymak000
@geoffreymak000 3 жыл бұрын
I think recursion is easier to reason, but that is just me. Depends on what language you use maybe recursion is easier to code, like scala or swift.
@martianstarslit3768
@martianstarslit3768 Жыл бұрын
Thanks for those videos. A lot of effort put into it to make it come live. Could you speak in consideration of non-native speakers? You seem quite too fast. Thank you.😊
@knightwing7894
@knightwing7894 5 жыл бұрын
thanks for the walkthrough. isn't this a BFS? pre-, in-, and postorder are DFS
@NickWhite
@NickWhite 5 жыл бұрын
KnightWing yep
@NickWhite
@NickWhite 5 жыл бұрын
Sorry about that I had bfs and dfs mixed up in a few of my videos when I first started these
@knightwing7894
@knightwing7894 5 жыл бұрын
public List postorder(Node root) { LinkedList result = new LinkedList(); postorderDFS(root, result); return result; } public static void postorderDFS(Node head, List list) { if(head == null) return; for(Node child : head.children) { postorderDFS(child, list); } list.add(head.val); }
@treyquattro
@treyquattro 4 жыл бұрын
Yes. Also "stack" is actually a queue (but that's probably implied in the BFS-DFS mix up that Nick already copped to)
@treyquattro
@treyquattro 4 жыл бұрын
@@knightwing7894 problem description asked for iterative. That ain't iterative.
@96merluzzo
@96merluzzo 4 жыл бұрын
Really spicy clap this time, well done
@jcmaybenot
@jcmaybenot 4 жыл бұрын
My lord and savior thank you.
@TreeLuvBurdpu
@TreeLuvBurdpu 2 жыл бұрын
You have like zero control over your audio volume between your different videos, I think
@pulkitgupta3477
@pulkitgupta3477 4 жыл бұрын
finally high audio
@gasal.c3675
@gasal.c3675 3 ай бұрын
ta da da da da..... keep going.. 😂 ( how loop work for bro.. )
@waeleldesoki1994
@waeleldesoki1994 5 жыл бұрын
thank you :))))
@charankumar1607
@charankumar1607 Жыл бұрын
Please explain a little bit more.
@zc7504
@zc7504 3 жыл бұрын
man I pray the god to kiss you
@killingjoyandmemeing
@killingjoyandmemeing 3 жыл бұрын
lol what
@vivek.tiwary
@vivek.tiwary 4 жыл бұрын
c# implementation: No need of a linked list I guess. /* // Definition for a Node. public class Node { public int val; public IList children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, IList _children) { val = _val; children = _children; } } */ public class Solution { //uncomment for rec solution // List ouput = new List(); public IList Postorder(Node root) { return PostOrderTraversal_Iter_590(root); } public List PostOrderTraversal_Iter_590(Node root) { Stack resultStack = new Stack(); if (root == null) { return resultStack.ToList(); } var nodeStack = new Stack(); nodeStack.Push(root);//1 added while (nodeStack.Count > 0) { var currentNode = nodeStack.Pop();//FirstLoop 1 poped --//2ndLoop 4 // 3rdLoop 2 // 4th loop 6 //5th loop 5 //1-->4-->2-->3-->6-->5 //ouput.Add(currentNode.val);//First loop 1 Added to output --// 2nd loop 4 added //3rd loop 2 added // 4th loop 3 added //4th loop 6 //5th loop 5added resultStack.Push(currentNode.val); foreach(Node nd in currentNode.children) { //FirstPass for 1 enter -- //4&2 not enter, will enter for 3 // will not enter for 6 & 5 nodeStack.Push(nd);//FirstPass 3&2&4 pushed //2nd Pass 5 & 6 will be pushed } } //return ouput.Reverse().ToList(); return resultStack.ToList(); } ////Recursive soluction // public List PostOrderTraversal_Rec_590(Node root) // { // if (root == null) // { // return ouput; // } // foreach (Node node in root.children) // { // PostOrderTraversal_Rec_590(node); // } // ouput.Add(root.val); // return ouput; // } }
How Many Balloons To Make A Store Fly?
00:22
MrBeast
Рет қаралды 163 МЛН
Self Taught Programmers... Listen Up.
11:21
Nick White
Рет қаралды 1,1 МЛН
N-ary Tree Postorder Traversal - Leetcode 590 - Python
9:50
NeetCodeIO
Рет қаралды 7 М.
2 Years of C++ Programming
8:20
Zyger
Рет қаралды 6 М.
145. Binary Tree Postorder Traversal - Day 25/31 Leetcode August Challenge
12:46
Programming Live with Larry
Рет қаралды 278
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 675 М.
All Software Developers NEED a Portfolio
14:57
Nick White
Рет қаралды 44 М.
Easy Google Coding Interview With Ben Awad
28:00
Clément Mihailescu
Рет қаралды 1 МЛН
N-ary Tree Preorder Traversal | Live Coding with Explanation | Leetcode - 589
10:04
LeetCode Palindrome Linked List Solution Explained - Java
9:35
Nick White
Рет қаралды 113 М.
How Many Balloons To Make A Store Fly?
00:22
MrBeast
Рет қаралды 163 МЛН