Leetcode - Flatten Binary Tree to Linked List (Python)

preview_player
Показать описание
May 2021 Leetcode Challenge
Leetcode - Flatten Binary Tree to Linked List #114
Difficulty: Medium
Рекомендации по теме
Комментарии
Автор

Great explanation dude. For some reason, I was struggling to understand the Neetcode solution (Which doesn't happen often), but you made it look so simple. Thanks again.

kashishmukheja
Автор

Great job! Keep doing the amazing video!

For space O(1), we may want to use "Stack".

1. init stack, push the root node

2. while loop here to check if there is still node in it

3. pop a node as the current node

3. for the current node, push right subtree FIRST, then left subtree

4. update the right subtree with the peek node in the stack (DO NOT POP)

5. update the left subtree as None

6. repeat step 2 ~ step 5

Here we have the result with space O(1)

xiaobaiyyy
welcome to shbcf.ru