Open In App

Replace node with depth in a binary tree

Last Updated : 21 Mar, 2023
Comments
Improve
Suggest changes
1 Like
Like
Report

Given a binary tree, replace each node with its depth value. For example, consider the following tree. Root is at depth 0, change its value to 0 and next level nodes are at depth 1 and so on. 

       3                       0
      /  \                    /   \
     2    5      == >;         1     1
   /   \                   /   \
  1     4                 2     2

The idea is to traverse tree starting from root. While traversing pass depth of node as parameter. We can track depth by passing it as 0 for root and one-plus-current-depth for children.

Below is the implementation of the idea. 

C++
// CPP program to replace every key value 
// with its depth.
#include<bits/stdc++.h>
using namespace std;

/* A tree node structure */
struct Node
{
    int data;
    struct Node *left, *right;
};

/* Utility function to create a
   new Binary Tree node */
struct Node* newNode(int data)
{
    Node *temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;    
    return temp;
}

// Helper function replaces the data with depth
// Note : Default value of level is 0 for root.
void replaceNode(struct Node *node, int level=0)
{
    // Base Case
    if (node == NULL)
        return;

    // Replace data with current depth
    node->data = level;

    replaceNode(node->left, level+1);
    replaceNode(node->right, level+1);
}

// A utility function to print inorder
// traversal of a Binary Tree
void printInorder(struct Node* node)
{
     if (node == NULL)
          return;
     printInorder(node->left);
     cout << node->data <<" ";
     printInorder(node->right);
}

/* Driver function to test above functions */
int main()
{
    struct Node *root = new struct Node;

    /* Constructing tree given in
       the above figure */
    root = newNode(3);
    root->left = newNode(2);
    root->right = newNode(5);
    root->left->left = newNode(1);
    root->left->right = newNode(4);
    
    cout << "Before Replacing Nodes\n";    
    printInorder(root);
    replaceNode(root);  
    cout << endl;
    
    cout << "After Replacing Nodes\n";
    printInorder(root);
    
    return 0;
}
Java Python3 C# JavaScript

Output
Before Replacing Nodes
1 2 4 3 5 
After Replacing Nodes
2 1 2 0 1 

Time Complexity: O(n) 
Space Complexity: If we don't consider size of stack for function calls then O(1) otherwise O(h)

Iterative Approach(Using Queue data structure):
Follow the below steps to solve the above problem:
1) Perform level order traversal with the help of queue data structure.
2) At each level keep track to level(indexing 0) and replace all nodes data of current level to level value.
3) Print the Inorder traversal of resultant tree.
Below is the implementation of above approach:

C++
// C++ Program to replace every node data
// with its depth
#include<bits/stdc++.h>
using namespace std;

// a tree node
struct Node{
    int data;
    struct Node *left, *right;
};

// utility function to create a new node
struct Node* newNode(int data){
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}

// Helper function replaces the data with depth
// Note : Default value of level is 0 for root.
void replaceNode(struct Node* root){
    // base case
    if(root == NULL) return;
    
    queue<Node*> q;
    int level = 0;
    q.push(root);
    while(!q.empty()){
        int n = q.size();
        for(int i = 0; i<n; i++){
            Node* front_node = q.front();
            q.pop();
            front_node->data = level;
            if(front_node->left) q.push(front_node->left);
            if(front_node->right) q.push(front_node->right);
        }
        level++;
    }
}

// A utility function to print inorder
// traversal of a Binary Tree
void printInorder(struct Node* node){
     if (node == NULL)
          return;
     printInorder(node->left);
     cout << node->data <<" ";
     printInorder(node->right);
}

// Driver function to test above functions 
int main(){
    struct Node *root = new struct Node;
    root = newNode(3);
    root->left = newNode(2);
    root->right = newNode(5);
    root->left->left = newNode(1);
    root->left->right = newNode(4);
     
    cout << "Before Replacing Nodes\n";   
    printInorder(root);
    
    replaceNode(root); 
    cout << endl;
     
    cout << "After Replacing Nodes\n";
    printInorder(root);
     
    return 0;
}
// THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGARWAL2852002)
Java Python3 C# JavaScript

Output
Before Replacing Nodes
1 2 4 3 5 
After Replacing Nodes
2 1 2 0 1 

Time Complexity: O(N) where N is the number of nodes in given binary tree
Auxiliary Space: O(N) due to queue data structure


Replace node with depth in a binary tree
Article Tags :
Practice Tags :

Similar Reads