Given a binary tree (or Binary Search Tree), convert it to a doubly-linked list. Can you do it inline?
Input:
60
50 70
40 55 65 75
Output:
60 -> 50 -> 70 ->40 -> 55 -> 65 -> 75
Approach
The structure of a Binary Tree Node is similar to Linked List Node. Binary Tree node has left and right. A linked list has prev and next.
Do a Bread-First-Search (BFS) and adjust the pointers as the nodes are read.
Solution
Input:
60
50 70
40 55 65 75
Output:
60 -> 50 -> 70 ->40 -> 55 -> 65 -> 75
Approach
The structure of a Binary Tree Node is similar to Linked List Node. Binary Tree node has left and right. A linked list has prev and next.
Do a Bread-First-Search (BFS) and adjust the pointers as the nodes are read.
Solution
No comments:
Post a Comment