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

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

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер
@priteshranoliya1969
@priteshranoliya1969 Жыл бұрын
ok! this is hard to understand..
@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
@rdwok14
@rdwok14 11 ай бұрын
The iterative solution has got to be at least a medium.
@birdbeakbeardneck3617
@birdbeakbeardneck3617 Жыл бұрын
Yep harder than it looks
@80rian-jeong
@80rian-jeong 5 күн бұрын
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
@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
@patrickfeeney4180
@patrickfeeney4180 8 ай бұрын
Nice. Very simple.
@haoyuguo3929
@haoyuguo3929 Жыл бұрын
using two stack is amazing.
@acecool1715
@acecool1715 11 ай бұрын
O_O, can't believe this really works O_O_O_O
@shanthureddy4234
@shanthureddy4234 Жыл бұрын
thanks for the explantion
@49-farhaanali86
@49-farhaanali86 5 ай бұрын
Lots of love man......!
@vivekmit06
@vivekmit06 Жыл бұрын
Thank you very much!!!
@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.
@dantedt3931
@dantedt3931 Жыл бұрын
Thanks dude
@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."
@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.
@atatekeli9295
@atatekeli9295 Жыл бұрын
What if we said stack.append(cur.right, cur.left) instead of stack.append(cur.right) and stack.append(cur.left) separately?
Жыл бұрын
Would you say that to set left and right pointers to null along the way is WRONG? Cause that makes it trivial
@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)
@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 8 ай бұрын
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
Iterative Postorder traversal of binary tree using one stack
14:05
Tushar Roy - Coding Made Simple
Рет қаралды 116 М.
Почему Катар богатый? #shorts
0:45
Послезавтра
Рет қаралды 2 МЛН
ССЫЛКА НА ИГРУ В КОММЕНТАХ #shorts
0:36
Паша Осадчий
Рет қаралды 8 МЛН
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 687 М.
5 Good Python Habits
17:35
Indently
Рет қаралды 621 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 147 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 576 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 642 М.
Iterative Postorder Traversal of a Binary Tree | Animation
27:56
Dinesh Varyani
Рет қаралды 16 М.
Почему Катар богатый? #shorts
0:45
Послезавтра
Рет қаралды 2 МЛН