Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python

  Рет қаралды 33,757

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 33
@ben1553
@ben1553 Жыл бұрын
Better solution for iterative is to reverse the solution you get from doing the reverse preorder traversal, no need for extra array.
@niranjanapn9641
@niranjanapn9641 9 ай бұрын
How did you come up with that? Could you please explain?
@davidteklea1032
@davidteklea1032 6 ай бұрын
No, you would have the left and right messed up
@yuwensun1006
@yuwensun1006 4 ай бұрын
@@davidteklea1032 you can modify the pre-order case by changing left and right, then reversing its output will give you the answer for post-order traversal.
@himage6540
@himage6540 Ай бұрын
​@@yuwensun1006true
@priteshranoliya1969
@priteshranoliya1969 Жыл бұрын
ok! this is hard to understand..
@Raf-s7g
@Raf-s7g Күн бұрын
thank you for not leaving me
@rdwok14
@rdwok14 11 ай бұрын
The iterative solution has got to be at least a medium.
@birdbeakbeardneck3617
@birdbeakbeardneck3617 Жыл бұрын
Yep harder than it looks
@neelmajm5868
@neelmajm5868 Жыл бұрын
I think it is better to create a tuple (node, False) instead of having two lists.
@antimuggle_ridhi2565
@antimuggle_ridhi2565 Жыл бұрын
same complexity still
@80rian-jeong
@80rian-jeong 9 күн бұрын
isn't this just like preorder traversal but left and right order switched and append to the result in reverse? below is the solution with very small modification from neetcode's preorder traversal class Solution: def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: res = [] stack = [] cur = root while cur or stack: if cur: res = [cur.val] + res stack.append(cur.left) cur = cur.right else: cur = stack.pop() return res
@haoyuguo3929
@haoyuguo3929 Жыл бұрын
using two stack is amazing.
@acecool1715
@acecool1715 11 ай бұрын
O_O, can't believe this really works O_O_O_O
@patrickfeeney4180
@patrickfeeney4180 8 ай бұрын
Nice. Very simple.
Жыл бұрын
Would you say that to set left and right pointers to null along the way is WRONG? Cause that makes it trivial
@shanthureddy4234
@shanthureddy4234 Жыл бұрын
thanks for the explantion
@yynnooot
@yynnooot 7 ай бұрын
I'm curious why you went with this approach as opposed to solutions that reverse the final result, or add to the front of the result list (instead of pushing to back)? Is that too 'hacky'? I feel like I'd start confusing myself during an interview while coming up with a solution like yours. It's very different than your Preorder Traversal approach
@GorgeousPuree
@GorgeousPuree 3 ай бұрын
Yes, because it is hacky. Essentially when you reverse the final result, you just get the right answer, but you don't really do the postorder traversal. Interviewer may not accept your code if you just reverse preorder traversal. Also here's a better explanation on why hacky solutions are not good: "Again, as I commented at in the most popular answer, strictly speaking, this solution for the post order is incorrect. Even though the final result is correct, imagine if there are topological dependencies among the nodes, the visiting order would be significant. Simply reversing the preorder result isn't right."
@49-farhaanali86
@49-farhaanali86 5 ай бұрын
Lots of love man......!
@vivekmit06
@vivekmit06 Жыл бұрын
Thank you very much!!!
@dantedt3931
@dantedt3931 Жыл бұрын
Thanks dude
@SahinSarkar-gr6vm
@SahinSarkar-gr6vm 11 ай бұрын
This soln doesn't have stack space complexity as height of tree, at least this specific implementation. I tried it with a tree of height 3, but I got height 7.
@tjalferes
@tjalferes Жыл бұрын
I like this solution, but I also like another solution: function postOrder(root) { if (!root) return []; const res = []; const S = [root]; while (S.length) { const i = S.pop(); res.unshift(i.data); if (i.left) S.push(i.left); if (i.right) S.push(i.right); } return res; }
@Kai-wi1md
@Kai-wi1md Жыл бұрын
Then you come up with the cost of the unshift() function which is O(m) - m is the length of the tree.
@swapnadeepmukherjee007
@swapnadeepmukherjee007 2 ай бұрын
@NeetCodeIO: The code is not fully visible on the screen and also hard to understand. Please fix the same.
@AustinWeeks
@AustinWeeks 2 ай бұрын
my friend be grateful that he even makes these videos the last line just says : vist.append(False)
@atatekeli9295
@atatekeli9295 Жыл бұрын
What if we said stack.append(cur.right, cur.left) instead of stack.append(cur.right) and stack.append(cur.left) separately?
@sangwookim5551
@sangwookim5551 Жыл бұрын
Hi I appreciate all of your videos. They have been immensely helpful. But one thing that I am dissatisfied is how many ads you put on your videos. There are many times after finishing 2 ads, another 2 ads run. Please help to fix this
@Sharmav03
@Sharmav03 8 ай бұрын
use brave bro or maybe an extension for yt ad block
@lalitvinde1441
@lalitvinde1441 9 ай бұрын
hello I get this solution but don't know the intuition for doing so if anyone can help me out def postorderTraversal(self, root): if not root: return [] stack = [root] result = [] while stack: current = stack.pop() result.append(current.val) if current.left: stack.append(current.left) if current.right: stack.append(current.right) return result[::-1]
@chrischika7026
@chrischika7026 8 ай бұрын
you are doing reverse preorder
Binary Search Tree Iterator - Leetcode 173 - Python
12:47
NeetCode
Рет қаралды 43 М.
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 2,1 МЛН
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 8 МЛН
Koko Eating Bananas - Binary Search - Leetcode 875 - Python
15:12
Iterative Postorder traversal of binary tree using one stack
14:05
Tushar Roy - Coding Made Simple
Рет қаралды 116 М.
The Best Way to Learn Linux
9:45
Mental Outlaw
Рет қаралды 129 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 585 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 740 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 150 М.
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 1,9 МЛН
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 2,1 МЛН