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 inputInput: 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