Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

129. Flatten Binary Tree to Linked List

MediumDay 38 of 60Trees & BST

Given the root of a binary tree, flatten it in place into a "linked list" made of the same nodes. Each node's right pointer should point to the next node and every left pointer should be null. The order of the nodes must be the preorder traversal of the original tree. The function returns nothing.

Example 1
Input: root = [1,2,5,3,4,null,6]
Output: [1,null,2,null,3,null,4,null,5,null,6]
Preorder is 1, 2, 3, 4, 5, 6, linked through right pointers.
Example 2
Input: root = []
Output: []
Constraints
  • 0 <= number of nodes <= 2000
  • -100 <= Node.val <= 100
Hints
Frequently asked at: Meta, Microsoft, Amazon
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min