Linked list to binary tree | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Linked list to binary tree

Hi I have node class which takes care of int as data and next pointer. Now same is used as member of mylinkedlist class. Can we use the same class linkedlist as binary tree ? What my thought was to have new class as binarytree with two members, mylinkedlist and node*. Is it okay or not ?

7th Sep 2022, 7:25 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Answers
+ 1
To do a binary tree you just need head node, being a node (left) value (right). Now, can you do a binary linked list with a linked list? Yes, but it's not the best way to implement it. Quick pseudo-code of a binary tree: Node { Node left; Node value; Node right; } Tree { Node head }
18th Sep 2022, 9:24 AM
Tomás Ribeiro
Tomás Ribeiro - avatar
+ 1
With a list would be: Node { int value; List<Node> son; } Tree { Node head; } This way you need to define the left son as index 0 and 1 as right son.
19th Sep 2022, 2:38 PM
Tomás Ribeiro
Tomás Ribeiro - avatar
+ 1
Okay...thanks a lot
20th Sep 2022, 1:25 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks Tomás Ribeiro Binary tree node is clear.. Could you plz elaborate on how to implement binary tree with list... i got that it is not best way but just curious to know how to do so
19th Sep 2022, 11:37 AM
Ketan Lalcheta
Ketan Lalcheta - avatar