Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python

  Рет қаралды 23,884

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 18
@NeetCodeIO
@NeetCodeIO Жыл бұрын
If you're looking for today's daily LC (Can Place Flowers) -> kzbin.info/www/bejne/kHjboqSgoc-ji6s
@victoriatfarrell
@victoriatfarrell Жыл бұрын
For me I think I prefer to append right and left to the stack and just loop while the stack is non-empty, starting by placing the root in the stack before the while loop. Makes it a very clean solution.
@hyperboliq
@hyperboliq 7 ай бұрын
I was wondering why he didn't do it that way. It was cool to learn another way though.
@RakimMiddya
@RakimMiddya 5 ай бұрын
How does this work?
@akhileshgotmare9812
@akhileshgotmare9812 4 ай бұрын
@@RakimMiddya here is the code I think @victoriatfarrell is referring to def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: if not root: return [] result = [] stack = [root] while stack: cur = stack.pop() result.append(cur.val) # Append right first, so that left is processed first if cur.right: stack.append(cur.right) if cur.left: stack.append(cur.left) return result
@aadill77
@aadill77 3 ай бұрын
Exactly I did the same.
@cyclone760
@cyclone760 Жыл бұрын
I have never thought of the call stack in this way before... great video
@saraahmed408
@saraahmed408 7 ай бұрын
Thank you so much for the explanation. But why the time complexity will be O(log n) if the tree was a balanced binary tree?
@ashok2089
@ashok2089 6 ай бұрын
It's not the Time Complexity, It's the space complexity he mentioned. So TC is O(n) as we must visit each node. SC is O(h), h is the height of the binary tree, and in the worst case SC could be O(n), in the case of a skewed binary tree. for balanced binary tree SC: O(log n).
@theboss-zk4wx
@theboss-zk4wx Жыл бұрын
thank you!
@gsivanithin
@gsivanithin Жыл бұрын
Great explanation! nice
@alexandersmirnov4274
@alexandersmirnov4274 Жыл бұрын
hi Why did you decide to open a new channel? Where should we expect new videos?
@gsivanithin
@gsivanithin Жыл бұрын
Easy to understand
@chandankhuntia7017
@chandankhuntia7017 Жыл бұрын
💞
@AbhishekAbhishek-dk2pi
@AbhishekAbhishek-dk2pi 4 ай бұрын
Its getting errors 🤦
@wavecheeez1246
@wavecheeez1246 8 күн бұрын
Im really tired of hearing from differen youtubers "its really easy". Its offensive to heare such. Even if its really easy and i know it im here not to take strike in to my dignity, people looking for a help and explenation but not to be frustrated
@wavecheeez1246
@wavecheeez1246 8 күн бұрын
And actually it not even close to easy to understand this... so its kind of mocking when saying "its easy"
Concatenation of Array - Leetcode 1929 - Python
4:00
NeetCodeIO
Рет қаралды 42 М.
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 764 М.
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 501 М.
Iterative Postorder traversal of binary tree using one stack
14:05
Tushar Roy - Coding Made Simple
Рет қаралды 117 М.
Binary Trees in Python: Introduction and Traversal Algorithms
28:40
LucidProgramming
Рет қаралды 216 М.
Check Completeness of a Binary Tree - Leetcode 958 - Python
7:57
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН