Left-Right representation of a binary tree is standard representation where every node has a pointer to left child and another pointer to right child.
Down-Right representation is an alternate representation where every node has a pointer to left (or first) child and another pointer to next sibling. So siblings at every level are connected from left to right.
Given a binary tree in left-right representation as below
1 / \ 2 3 / \ 4 5 / / \ 6 7 8
Convert the structure of the tree to down-right representation like the below tree.
1 | 2 – 3 | 4 — 5 | | 6 7 – 8
The conversion should happen in-place, i.e., left child pointer should be used as down pointer and right child pointer should be used as right sibling pointer.
We strongly recommend to minimize your browser and try this yourself.
The idea is to first convert left and right children, then convert the root. Following is C++ implementation of the idea.
Traversal of the tree converted to down-right form 1 2 3 4 5 7 8 6
Time complexity of the above program is O(n).
This article is contributed by Abhishek. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Minimum swap required to convert binary tree to binary search tree
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Convert a given Binary tree to a tree that holds Logical AND property
- Construct Binary Tree from String with bracket representation
- Construct Binary Tree from given Parent Array representation
- Shortest path between two nodes in array like representation of binary tree
- Construct Complete Binary Tree from its Linked List Representation
- Convert a Binary Tree into its Mirror Tree
- Convert Ternary Expression to a Binary Tree
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Convert a given Binary Tree to Doubly Linked List | Set 2