Reverse alternate levels of a perfect binary tree

504 篇文章 0 订阅

转自:http://www.geeksforgeeks.org/reverse-alternate-levels-binary-tree/


Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree.

  
Given tree: 
               a
            /     \
           b       c
         /  \     /  \
        d    e    f    g
       / \  / \  / \  / \
       h  i j  k l  m  n  o 

Modified tree:
  	       a
            /     \
           c       b
         /  \     /  \
        d    e    f    g
       / \  / \  / \  / \
      o  n m  l k  j  i  h 

We strongly recommend to minimize the browser and try this yourself first.

simple solution is to do following steps.
1) Access nodes level by level.
2) If current level is odd, then store nodes of this level in an array.
3) Reverse the array and store elements back in tree.

tricky solution is to do two inorder traversals. Following are steps to be followed.
1) Traverse the given tree in inorder fashion and store all odd level nodes in an auxiliary array. For the above example given tree, contents of array become {h, i, b, j, k, l, m, c, n, o}

2) Reverse the array. The array now becomes {o, n, c, m, l, k, j, b, i, h}

3) Traverse the tree again inorder fashion. While traversing the tree, one by one take elements from array and store elements from array to every odd level traversed node.
For the above example, we traverse ‘h’ first in above array and replace ‘h’ with ‘o’. Then we traverse ‘i’ and replace it with n.

Following is C++ implementation of the above algorithm.

// C++ program to reverse alternate levels of a binary tree
#include<iostream>
#define MAX 100
using namespace std;
 
// A Binary Tree node
struct Node
{
     char data;
     struct Node *left, *right;
};
 
// A utility function to create a new Binary Tree Node
struct Node *newNode( char item)
{
     struct Node *temp =  new Node;
     temp->data = item;
     temp->left = temp->right = NULL;
     return temp;
}
 
// Function to store nodes of alternate levels in an array
void storeAlternate(Node *root, char arr[], int *index, int l)
{
     // Base case
     if (root == NULL) return ;
 
     // Store elements of left subtree
     storeAlternate(root->left, arr, index, l+1);
 
     // Store this node only if this is a odd level node
     if (l%2 != 0)
     {
         arr[*index] = root->data;
         (*index)++;
     }
 
     // Store elements of right subtree
     storeAlternate(root->right, arr, index, l+1);
}
 
// Function to modify Binary Tree (All odd level nodes are
// updated by taking elements from array in inorder fashion)
void modifyTree(Node *root, char arr[], int *index, int l)
{
     // Base case
     if (root == NULL) return ;
 
     // Update nodes in left subtree
     modifyTree(root->left, arr, index, l+1);
 
     // Update this node only if this is an odd level node
     if (l%2 != 0)
     {
         root->data = arr[*index];
         (*index)++;
     }
 
     // Update nodes in right subtree
     modifyTree(root->right, arr, index, l+1);
}
 
// A utility function to reverse an array from index
// 0 to n-1
void reverse( char arr[], int n)
{
     int l = 0, r = n-1;
     while (l < r)
     {
         int temp = arr[l];
         arr[l] = arr[r];
         arr[r] = temp;
         l++; r--;
     }
}
 
// The main function to reverse alternate nodes of a binary tree
void reverseAlternate( struct Node *root)
{
     // Create an auxiliary array to store nodes of alternate levels
     char *arr = new char [MAX];
     int index = 0;
 
     // First store nodes of alternate levels
     storeAlternate(root, arr, &index, 0);
 
     // Reverse the array
     reverse(arr, index);
 
     // Update tree by taking elements from array
     index = 0;
     modifyTree(root, arr, &index, 0);
}
 
// A utility function to print indorder traversal of a
// binary tree
void printInorder( struct Node *root)
{
     if (root == NULL) return ;
     printInorder(root->left);
     cout << root->data << " " ;
     printInorder(root->right);
}
 
// Driver Program to test above functions
int main()
{
     struct Node *root = newNode( 'a' );
     root->left = newNode( 'b' );
     root->right = newNode( 'c' );
     root->left->left = newNode( 'd' );
     root->left->right = newNode( 'e' );
     root->right->left = newNode( 'f' );
     root->right->right = newNode( 'g' );
     root->left->left->left = newNode( 'h' );
     root->left->left->right = newNode( 'i' );
     root->left->right->left = newNode( 'j' );
     root->left->right->right = newNode( 'k' );
     root->right->left->left = newNode( 'l' );
     root->right->left->right = newNode( 'm' );
     root->right->right->left = newNode( 'n' );
     root->right->right->right = newNode( 'o' );
 
     cout << "Inorder Traversal of given tree\n" ;
     printInorder(root);
 
     reverseAlternate(root);
 
     cout << "\n\nInorder Traversal of modified tree\n" ;
     printInorder(root);
 
     return 0;
}

Output:

Inorder Traversal of given tree
h d i b j e k a l f m c n g o

Inorder Traversal of modified tree
o d n c m e l a k f j b i g h

Time complexity of the above solution is O(n) as it does two inorder traversals of binary tree.

This article is contributed by Kripal Gaurav. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值