Convert left-right representation of a binary tree to down-right
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.
Algorithm:
- Define a struct for the binary tree node with integer key, and left and right child pointers.
- Define a recursive function convert that takes the root of the binary tree as input.
- In the convert function, check if the root is NULL. If it is, return.
- Recursively call convert on the left child of the root.
- Recursively call convert on the right child of the root.
- If the left child of the root is NULL, set the left child to be the right child.
- Otherwise, set the right child of the left child to be the right child.
- Set the right child of the root to be NULL.
- Define a utility function downRightTraversal that takes the root of the binary tree as input and traverses the tree in the down-right order.
- In the downRightTraversal function, if the root is not NULL, print the key of the root, then recursively call downRightTraversal on the right child of the root, and then recursively call downRightTraversal on the left child of the root.
- Define a utility function newNode that takes an integer key as input and returns a new binary tree node with that key and left and right child pointers set to NULL.
- In the main function, create a binary tree with the same structure as the one shown in the comment above the tree diagram in the code.
- Call the convert function on the root of the binary tree.
- Call the downRightTraversal function on the root of the binary tree to print the tree in down-right order.
- Return 0 to indicate successful program execution.
C++
/* C++ program to convert left-right to down-right representation of binary tree */#include <bits/stdc++.h>using namespace std;// A Binary Tree Nodestruct node{ int key; struct node *left, *right;};// An Iterative level order traversal based function to// convert left-right to down-right representation.void convert(node *root){ // Base Case if (root == NULL) return; // Recursively convert left an right subtrees convert(root->left); convert(root->right); // If left child is NULL, make right child as left // as it is the first child. if (root->left == NULL) root->left = root->right; // If left child is NOT NULL, then make right child // as right of left child else root->left->right = root->right; // Set root's right as NULL root->right = NULL;}// A utility function to traverse a tree stored in// down-right form.void downRightTraversal(node *root){ if (root != NULL) { cout << root->key << " "; downRightTraversal(root->right); downRightTraversal(root->left); }}// Utility function to create a new tree nodenode* newNode(int key){ node *temp = new node; temp->key = key; temp->left = temp->right = NULL; return temp;}// Driver program to test above functionsint main(){ // Let us create binary tree shown in above diagram /* 1 / \ 2 3 / \ 4 5 / / \ 6 7 8 */ node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->right->left = newNode(4); root->right->right = newNode(5); root->right->left->left = newNode(6); root->right->right->left = newNode(7); root->right->right->right = newNode(8); convert(root); cout << "Traversal of the tree converted to down-right form\n"; downRightTraversal(root); return 0;} |
Java
/* Java program to convert left-right todown-right representation of binary tree */class GFG{// A Binary Tree Nodestatic class node{ int key; node left, right; node(int key) { this.key = key; this.left = null; this.right = null; }}// An Iterative level order traversal// based function to convert left-right// to down-right representation.static void convert(node root){ // Base Case if (root == null) return; // Recursively convert left // an right subtrees convert(root.left); convert(root.right); // If left child is NULL, make right // child as left as it is the first child. if (root.left == null) root.left = root.right; // If left child is NOT NULL, then make // right child as right of left child else root.left.right = root.right; // Set root's right as NULL root.right = null;}// A utility function to traverse a// tree stored in down-right form.static void downRightTraversal(node root){ if (root != null) { System.out.print(root.key + " "); downRightTraversal(root.right); downRightTraversal(root.left); }}// Utility function to create// a new tree nodestatic node newNode(int key){ node temp = new node(0); temp.key = key; temp.left = null; temp.right = null; return temp;}// Driver Codepublic static void main(String[] args){ // Let us create binary tree // shown in above diagram /* 1 / \ 2 3 / \ 4 5 / / \ 6 7 8 */ node root = new node(1); root.left = newNode(2); root.right = newNode(3); root.right.left = newNode(4); root.right.right = newNode(5); root.right.left.left = newNode(6); root.right.right.left = newNode(7); root.right.right.right = newNode(8); convert(root); System.out.println("Traversal of the tree " + "converted to down-right form"); downRightTraversal(root);}}// This code is contributed// by Prerna Saini |
Python3
# Python3 program to convert left-right to# down-right representation of binary tree# Helper function that allocates a new# node with the given data and None# left and right pointers. class newNode: # Construct to create a new node def __init__(self, key): self.key = key self.left = None self.right = None# An Iterative level order traversal based# function to convert left-right to down-right# representation.def convert(root): # Base Case if (root == None): return # Recursively convert left an # right subtrees convert(root.left) convert(root.right) # If left child is None, make right # child as left as it is the first child. if (root.left == None): root.left = root.right # If left child is NOT None, then make # right child as right of left child else: root.left.right = root.right # Set root's right as None root.right = None# A utility function to traverse a# tree stored in down-right form.def downRightTraversal(root): if (root != None): print( root.key, end = " ") downRightTraversal(root.right) downRightTraversal(root.left)# Driver Codeif __name__ == '__main__': # Let us create binary tree shown # in above diagram """ 1 / \ 2 3 / \ 4 5 / / \ 6 7 8 """ root = newNode(1) root.left = newNode(2) root.right = newNode(3) root.right.left = newNode(4) root.right.right = newNode(5) root.right.left.left = newNode(6) root.right.right.left = newNode(7) root.right.right.right = newNode(8) convert(root) print("Traversal of the tree converted", "to down-right form") downRightTraversal(root)# This code is contributed by# Shubham Singh(SHUBHAMSINGH10) |
C#
// C# program to convert left-right to// down-right representation of binary treeusing System;class GFG{// A Binary Tree Nodepublic class node{ public int key; public node left, right; public node(int key) { this.key = key; this.left = null; this.right = null; }}// An Iterative level order traversal// based function to convert left-right// to down-right representation.public static void convert(node root){ // Base Case if (root == null) { return; } // Recursively convert left // an right subtrees convert(root.left); convert(root.right); // If left child is NULL, make right // child as left as it is the first child. if (root.left == null) { root.left = root.right; } // If left child is NOT NULL, then make // right child as right of left child else { root.left.right = root.right; } // Set root's right as NULL root.right = null;}// A utility function to traverse a// tree stored in down-right form.public static void downRightTraversal(node root){ if (root != null) { Console.Write(root.key + " "); downRightTraversal(root.right); downRightTraversal(root.left); }}// Utility function to create// a new tree nodepublic static node newNode(int key){ node temp = new node(0); temp.key = key; temp.left = null; temp.right = null; return temp;}// Driver Codepublic static void Main(string[] args){ // Let us create binary tree // shown in above diagram /* 1 / \ 2 3 / \ 4 5 / / \ 6 7 8 */ node root = new node(1); root.left = newNode(2); root.right = newNode(3); root.right.left = newNode(4); root.right.right = newNode(5); root.right.left.left = newNode(6); root.right.right.left = newNode(7); root.right.right.right = newNode(8); convert(root); Console.WriteLine("Traversal of the tree " + "converted to down-right form"); downRightTraversal(root);}}// This code is contributed// by Shrikant13 |
Javascript
<script> // JavaScript program to convert left-right to// down-right representation of binary tree// A Binary Tree Nodeclass node{ constructor(key) { this.key = key; this.left = null; this.right = null; }}// An Iterative level order traversal// based function to convert left-right// to down-right representation.function convert(root){ // Base Case if (root == null) { return; } // Recursively convert left // an right subtrees convert(root.left); convert(root.right); // If left child is NULL, make right // child as left as it is the first child. if (root.left == null) { root.left = root.right; } // If left child is NOT NULL, then make // right child as right of left child else { root.left.right = root.right; } // Set root's right as NULL root.right = null;}// A utility function to traverse a// tree stored in down-right form.function downRightTraversal(root){ if (root != null) { document.write(root.key + " "); downRightTraversal(root.right); downRightTraversal(root.left); }}// Utility function to create// a new tree nodefunction newNode(key){ var temp = new node(0); temp.key = key; temp.left = null; temp.right = null; return temp;}// Driver Code// Let us create binary tree// shown in above diagram/* 1 / \ 2 3 / \ 4 5 / / \ 6 7 8*/var root = new node(1);root.left = newNode(2);root.right = newNode(3);root.right.left = newNode(4);root.right.right = newNode(5);root.right.left.left = newNode(6);root.right.right.left = newNode(7);root.right.right.right = newNode(8);convert(root);document.write("Traversal of the tree " + "converted to down-right form<br>");downRightTraversal(root);</script> |
Traversal of the tree converted to down-right form 1 2 3 4 5 7 8 6
Time Complexity: O(n)
Space Complexity: O(n)






















Please Login to comment...