Convert left | C ++ program to convert left - right to down - right representation of binary tree ; A Binary Tree Node ; An Iterative level order traversal based function to convert left - right to down - right representation . ; Base Case ; Recursively convert left an right subtrees ; If left child is NULL , make right child as left as it is the first child . ; If left child is NOT NULL , then make right child as right of left child ; Set root 's right as NULL ; A utility function to traverse a tree stored in down - right form . ; Utility function to create a new tree node ; Driver program to test above functions ; 1 / \ 2 3 / \ 4 5 / / \ 6 7 8
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int key ; struct node * left , * right ; } ; void convert ( node * root ) { if ( root == NULL ) return ; convert ( root -> left ) ; convert ( root -> right ) ; if ( root -> left == NULL ) root -> left = root -> right ; else root -> left -> right = root -> right ; root -> right = NULL ; } void downRightTraversal ( node * root ) { if ( root != NULL ) { cout << root -> key << " ▁ " ; downRightTraversal ( root -> right ) ; downRightTraversal ( root -> left ) ; } } node * newNode ( int key ) { node * temp = new node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { 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 STRNEWLINE " ; downRightTraversal ( root ) ; return 0 ; }