Diagonal Traversal of Binary Tree
Given a binary tree, print the diagonal traversal of the binary tree.
Example:
Consider lines of slope -1 passing between nodes. Given a Binary Tree, print all diagonal elements in a binary tree belonging to same line.

In the above example lines of slope -1 are passed between nodes and the diagonal traversal will be: 2 5 9 7 6 11 4 2 5
Pre-requisite:
Queue q, root of binary tree, Node* temp, Node* temp1
Algorithm:
The algorithm is actually processing the right children and EnQueueing the left children for each parent node. The EnQueued children accts as nodes to be processed for next level.
FUNCTION diagonalPrint(root) EnQueue (q, root); While (q is not empty){ temp=DeQueue(q); Print temp->data; temp1=temp; While (temp1 is not NULL) IF (temp1->left is not NULL) //if left child exists EnQueue (q, temp1->left); //enqueue END IF temp1=temp1->right; //traverse to right child IF (temp1 is not NULL) Print temp1->data; END IF END While END While END FUNCTIONExample with explanation:
Nodes are represented with their respective values for better understanding.
C++ implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer