bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 114. Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List

Leetcode Solutions

Approach: O() Iterative Solution

  1. Start with the currentNode as the root of the tree.
  2. While currentNode is not null, check if it has a left child.
  3. If there is no left child, move to the right child by setting currentNode to currentNode.right.
  4. If there is a left child, find the rightmost node in the left subtree.
  5. Connect the rightmost node's right child to the currentNode's right child.
  6. Set the currentNode's right child to its left child.
  7. Set the currentNode's left child to null.
  8. Move to the right child by setting currentNode to currentNode.right.
  9. Repeat the process until all nodes have been visited.
UML Thumbnail

Approach: Recursion

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...