254. Binary Tree to Doubly Linked List

Given a Binary Tree, convert it into a Doubly Linked List (DLL) in-place. The conversion should maintain the in-order traversal of the binary tree. In the DLL:

  • The left pointer of each node should point to the previous node.
  • The right pointer of each node should point to the next node.

Return the head node of the resulting doubly linked list.

You must not create any new nodes — only modify the left and right pointers of existing tree nodes.

Example 1:

Input: root = [10,12,15,25,30,36]

Output: [25, 12, 30, 10, 36, 15]

Explanation:

  • In-order traversal: 25 → 12 → 30 → 10 → 36 → 15
  • DLL structure: 25 <-> 12 <-> 30 <-> 10 <-> 36 <-> 15

Example 2:

Input: root = [1,null,2,null,3]

Output: [1, 2, 3]

Explanation:

  • Right-skewed tree. In-order: 1 → 2 → 3
  • DLL: 1 <-> 2 <-> 3

Now Your Turn!

Pick the correct output for the given input

Input: root = [3,2,null,1]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= Number of Nodes <= 10⁓
  • Node values are unique

Fun Facts

0
class Solution {
public:
TreeNode* bToDLL(TreeNode* root) {
// Your code goes here
}
};
Test Case

Input:

Root