- Write a JavaScript Program to convert a Binary Tree to Doubly Linked
List?
<script>
class Node {
constructor(data)
{
this.data = data;
this.left = this.right = null;
}
}
var root;
var head;
function BToDLL( root)
{
if (root == null)
return;
BToDLL(root.right);
root.right = head;
if (head != null)
head.left = root;
head = root;
BToDLL(root.left);
}
function printList( head)
{
document.write("Extracted Double Linked List is :<br> "");
while (head != null) {
document.write(head.data + "");
head = head.right;
}
}
root = new Node(5);
root.left = new Node(3);
root.right = new Node(6);
root.left.right = new Node(4);
root.left.left = new Node(1);
root.right.right = new Node(8);
root.left.left.right = new Node(2);
root.left.left.left = new Node(0);
root.right.right.left = new Node(7);
root.right.right.right = new Node(9);
BToDLL(root);
printList(head);
</script>