Iterative Postorder traversal of binary tree using one stack

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

Postorder traversal of binary tree using one stack.
Рекомендации по теме
Комментарии
Автор

I almost got the logic while thinking of the problem. I couldn't think of that inner while loop to break the loop when we are done with all the lefts. Though I could understand your initial explanation, its little tough to match the same with the code. Thanks for the great explanation!!.

santhosh
Автор

Thanks for the explanation! You are saving a lot of time for me. Instead of reading articles or reading codes sometimes, i can simply watch your video . I implemented the code on my own after this video .

Code (Java):-
public static void root)
{
Stack<TreeNode> s = new Stack<>();
TreeNode curr = root;
while(curr != null || !s.isEmpty())
{
while(curr != null)
{
s.push(curr);
curr = curr.left;
}

if(!s.isEmpty())
{
//check is stack top has right child
if(s.peek().right != null)
{
= s.peek().right;
}
else
{
temp = s.pop();
");
> 0 && s.peek().right == temp)

= s.pop();
");

}

}
}

uditgaba
Автор

Tushar, this was an amazing visual walkthrough followed by a brilliant code walkthrough. Thanks for taking the time to share :)

ghostpieces
Автор

Best explanation, I have watched so far!

meghnasrivastava
Автор

almost got the logic and was stuck at how to check if right is already visited or not. This explains it bang on. Thank u

kiratsaluja
Автор

Thank you, better than the other explanations I have watched or read.

vvwu
Автор

super explanation..thanks for it..please do more videos on BST, AVL and red-black tree

yelururao
Автор

Clear and intuitive explanation! Thank you Tushar.

mingxiao
Автор

Thank you for making learning so much easier :)

shreyasingh-snbs
Автор

straigh forward way ....nice....instead of using previous variable or skip counts....this looks clean

sureshiva
Автор

Great walkthrough. Only thing I want to point out is offer and poll are methods of queue API. We should be using push and pop of stack API.

sameer_sah
Автор

Possibly the clearest iterative postorder traversal of trees shown on Youtube! Will this technique of using one stack work if there are 3 or more child nodes for each parent, or would a two-stack solution be needed then?

TwinbeeUK
Автор

Great Video Tushar. In your code, you used, Stack<TreeNode> stack = new LinkedList<>().. Is this correct?

karthickm
Автор

exceptional explanation.... The algo explanation was so good, that i could code it just by looking halfway through the off to the amazing work :)

shivamdixit
Автор

Awsome tutorial. well explained. Helped me a lot . Thank you Tushar.

JangBahadur
Автор

A quick question :: you used or in the while condition instead of using and so, whenever the curr will be null the loop will break, without giving the correct output. This thing kind of contradicts with your algorithm.

utkarshdwivedi
Автор

Thank you Tushar, very clear. Understand better now.

chenboxie
Автор

This iterative traversal has been messing up my mind for long, not anymore.👍

abhinav_
Автор

Thank you this was very helpful. Given that space and time complexity are the same using a single stack vs two stacks (please, correct me if I'm wrong), are there any advantages/disadvantages for using a single stack vs two stacks?

jbs
Автор

dude you're a legend . thank you !

SonGoku-epwj
welcome to shbcf.ru