If you're looking for today's daily LC (Can Place Flowers) -> kzbin.info/www/bejne/kHjboqSgoc-ji6s
@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.
@servantofthelord81474 ай бұрын
I was wondering why he didn't do it that way. It was cool to learn another way though.
@RakimMiddya2 ай бұрын
How does this work?
@akhileshgotmare98122 ай бұрын
@@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Ай бұрын
Exactly I did the same.
@cyclone760 Жыл бұрын
I have never thought of the call stack in this way before... great video
@saraahmed4085 ай бұрын
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?
@ashok20894 ай бұрын
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).
@gsivanithin Жыл бұрын
Great explanation! nice
@theboss-zk4wx Жыл бұрын
thank you!
@gsivanithin Жыл бұрын
Easy to understand
@alexandersmirnov4274 Жыл бұрын
hi Why did you decide to open a new channel? Where should we expect new videos?