Q. Program to convert a given binary tree to doubly linked list.

Explanation

In this program, we need to convert the given binary tree to corresponding doubly liked list.

The binary tree is a tree data structure in which each node has at most two children node.

This can be achieved by traversing the tree in the in-order manner that is, left the child -> root ->right node. Traverse left sub-tree and convert it into the doubly linked list by adding nodes to the end of the list. In this way, leftmost node will become head of the list. Then, convert the right sub-tree into the doubly linked list.

Binary tree:

Algorithm

1. Define a Node class which represents a node in the binary tree. It will have three properties: data left, and right where the left and right represent two children of a node.
2. Root will represent the root of the binary tree. Head and tail node represent the head and tail of the doubly linked list.
3. BinaryTreeToDLL() will convert the given binary tree to corresponding doubly linked list.
1. Perform inorder traversal of the binary tree by converting the left subtree first.
2. If the list is empty, both head and tail will point to a node.
3. If the list is not empty, the node will be inserted at the end of the list. Here, pointer left, and right will represent the previous and next pointer of the doubly linked list.
4. Now, recursively call BinaryTreeToDLL() to convert the right subtree.
4. display() will show all the nodes present in the list.
1. Define a new node 'current' that will point to the head.
2. Print current.data till current points to null.
3. Current will point to the next node in the list in each iteration.

Solution

Python

Output:

Nodes of generated doubly linked list:
4 2 5 1 6 3 7

C

Output:

Nodes of generated doubly linked list:
4 2 5 1 6 3 7

JAVA

Output:

Nodes of generated doubly linked list:
4 2 5 1 6 3 7

C#

Output:

Nodes of generated doubly linked list:
4 2 5 1 6 3 7

PHP

Output:

Nodes of generated doubly linked list:
4 2 5 1 6 3 7

Next Topic#
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.javatpoint.com/program-to-convert-a-given-binary-tree-to-doubly-linked-list", "fetch_time": 1568550317000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2019-39/segments/1568514571360.41/warc/CC-MAIN-20190915114318-20190915140318-00257.warc.gz", "warc_record_offset": 810579983, "warc_record_length": 13065, "token_count": 495, "char_count": 2035, "score": 3.5625, "int_score": 4, "crawl": "CC-MAIN-2019-39", "snapshot_type": "longest", "language": "en", "language_score": 0.8221029043197632, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00014-of-00064.parquet"}