# Diagonal Traversal of Binary Tree in C++?

C++Server Side ProgrammingProgramming

To consider the nodes that are passing between lines of slope -1. The diagonal traversal of the binary tree will be to traverse and print all those nodes present between these lines.

Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.

struct Node {
int data;
struct Node *leftChild, *rightChild;
};

Next we create our createNode(int data) function that takes an int value and assign it to the data member of the node. The function returns the pointer to the created struct Node. Also, the left and right child of the newly created node are set to null.

struct Node* newNode(int data){
struct Node* node = new Node();
node->data = data;
node->leftChild = node->rightChild = NULL;
return node;
}

The traverseDiagonal(Node* root, int depth, map<int, vector<int>> &myMap) function takes our root node, the current depth and a map that has int values as key and vector of int as the value . We pass the myMap as reference to this function. The function then checks if the root is null or not and if it isn’t then we push the element root->data at the back of our vector data structure at current depth as we perform the inorder traversal.

void traverseDiagonal(Node* root, int depth, map<int, vector<int>> &m){
if(root){
m[depth].push_back(root->data);

We then do a recursive inorder traversal of the tree tracking the diagonal distance and add 1 to the depth whenever we traverse the left child of the tree. We don’t add anything to depth when we traverse the right child of the tree.

traverseDiagonal(root->leftChild, depth+1, myMap);
traverseDiagonal(root->rightChild, depth, myMap);

Next, in our main function we create the tree using the createNode(data) function.

Node *root = createNode(10);
root->leftChild = createNode(5);
root->rightChild = createNode(15);
root->leftChild->leftChild = createNode(4);
root->leftChild->rightChild = createNode(6);
root->rightChild->rightChild = createNode(17);
root->rightChild->rightChild->leftChild = createNode(16);

We then declare a map myMap containing int as key and vector of int as value. This map is then passed to the traverseDiagonal along with the root node and the current depth.

map<int, vector<int>> myMap;
traverseDiagonal(root, 0, myMap);

After the map myMap is filled with value we iterate upon it using the range based for loop and print those diagonal values.

for(auto k: myMap){
for(auto Nodes: k.second)
cout<<Nodes<<" ";
cout<<endl;
}

## Example

Let us look at the following implementation to find the diagonal traversal of binary tree.

#include <iostream>
#include <map>
#include <vector>
using namespace std;
struct Node{
int data;
Node *leftChild, *rightChild;
};
Node* createNode(int data){
Node* node = new Node();
node->data = data;
node->leftChild = node->rightChild = NULL;
return node;
}
void traverseDiagonal(Node* root, int depth, map<int, vector<int>> &myMap){
if(root){
m[depth].push_back(root->data);
traverseDiagonal(root->leftChild, depth+1, myMap);
traverseDiagonal(root->rightChild, depth, myMap);
}
}
int main(){
Node *root = createNode(10);
root->leftChild = createNode(5);
root->rightChild = createNode(15);
root->leftChild->leftChild = createNode(4);
root->leftChild->rightChild = createNode(6);
root->rightChild->rightChild = createNode(17);
root->rightChild->rightChild->leftChild = createNode(16);
map<int, vector<int>> myMap;
traverseDiagonal(root, 0, myMap);
for(auto k: myMap){
for(auto Nodes: k.second)
cout<<Nodes<<" ";
cout<<endl;
}
}

## Output

The above code will produce the following output −

10 15 17
5 6 16
4
Published on 16-Jan-2021 14:57:25