LeetCode N-ary Tree Postorder Traversal Solution Explained - Java

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


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

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

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

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

dude massively appreciate these videos!

milad
Автор

Thank you my code teacher for always doing an amazing work. Look at this solution. Has a runtime of 1 ms.


class Solution {
List<Integer> ans = new ArrayList<>();
public List<Integer> postorder(Node root) {
if(root == null) return ans;

for(Node child : root.children){
postorder(child);
}
ans.add(root.val);
return ans;
}

}

simonkaranja
Автор

Good Job Nick. Helps me to wrap my head around problems quickly

sharidass
Автор

Dude you gotta manage your mic clipping the output. Great content, but hard to listen to.

nobodyislistining
Автор

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 :)

antonantochi
Автор

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

AnotherCogInTheMurderMachine
Автор

Another way to solve this in c#:
public class Solution {
public IList<int> Postorder(Node root) {
IList<int> res = new List<int>();
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<int> res){
for(int
addData(node.children[i], res);
res.Add(node.val);
}
}

mohammedghabyen
Автор

Great video! Is there any specific reason you didnt just use a normal stack, Nick?

Matlaps
Автор

do the tech companies allow to use
c++ stl and other utility functions during tech interview???

albert_schneider
Автор

class Solution {
public List<Integer> postorder(Node root) {

List<Integer> result = new LinkedList<>();
process(root, result);
return result;

}

private void process(Node root, List<Integer> result) {
if(root == null) {
return;
}

for(Node child : root.children) {
process(child, result);
}

result.add(root.val);
}
}

RBNrocks
Автор

How is the "for" loop not throwing a Null Pointer exception when "node.children" is null?

spandiar
Автор

Name of stack was bad naming. Its a friggen linkedlist. However it behaves like a stack.

Turnpost
Автор

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.

hannikyaw
Автор

Thanks for making these vids. Stack is LIFO, yours is just a backwards queue, not a stack, because it’s a FIFO implementation.

javaluvawithjeremystones
Автор

thanks for the walkthrough. isn't this a BFS? pre-, in-, and postorder are DFS

knightwing
Автор

Really spicy clap this time, well done

merluzzo
Автор

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.😊

martianstarslit
Автор

ta da da da keep going.. 😂 ( how loop work for bro.. )

gasal.c
Автор

You have like zero control over your audio volume between your different videos, I think

TreeLuvBurdpu
Автор

c# implementation: No need of a linked list I guess.

/*
// Definition for a Node.
public class Node {
public int val;
public IList<Node> children;

public Node() {}

public Node(int _val) {
val = _val;
}

public Node(int _val, IList<Node> _children) {
val = _val;
children = _children;
}
}
*/

public class Solution {
//uncomment for rec solution
// List<int> ouput = new List<int>();

public IList<int> Postorder(Node root) {
return
}
public List<int> root) {
Stack<int> resultStack = new Stack<int>();
if (root == null) {
return resultStack.ToList();
}

var nodeStack = new Stack<Node>();
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
loop 1 Added to output --// 2nd loop 4 added //3rd loop 2 added // 4th loop 3 added //4th loop 6 //5th loop 5added


foreach(Node nd in currentNode.children) { //FirstPass for 1 enter -- //4&2 not enter, will enter for 3 // will not enter for 6 & 5
3&2&4 pushed //2nd Pass 5 & 6 will be pushed
}
}

//return
return resultStack.ToList();

}

////Recursive soluction
// public List<int> root)
// {
// if (root == null)
// {
// return ouput;
// }
// foreach (Node node in root.children)
// {
//
// }
// ouput.Add(root.val);

// return ouput;

// }

}

vivek.tiwary