Related Articles
Triple Order Traversal of a Binary Tree
• Last Updated : 17 Aug, 2020

Given a Binary Tree, the task is to find it’s Triple Order Traversal

Triple Order Traversal is a tree traversal technique in which every node is traversed thrice in the following order:

• Visit the root node
• Traverse the left subtree
• Visit the root node
• Traverse the right subtree
• Visit the root node.

Examples:

```Input:
A
/ \
B   C
/ \   \
F   D   E

Output: A B F F F B D D D B A C C E E E C A

Input:
A
/ \
B   C
/ \
E   D
/
F
Output: A B E F F F E E B D D D B A C C C A
```

Approach:
Follow the steps below to solve the problem:

• Start the traversal from the root.
• If the current node does not exist, simply return from it.
• Otherwise:
• Print the value of the current node.
• Recursively traverse the left subtree.
• Again, print the current node.
• Recursively traverse the right subtree.
• Again, print the current node.
• Repeat the above steps until all nodes in the tree are visited.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement triple``// order traversal of a binary tree``#include ``using` `namespace` `std;`` ` `// Structure of a Node``struct` `node {``    ``char` `data;``    ``struct` `node *left, *right;``};`` ` `// Function to create new node``struct` `node* newNode(``char` `ch)``{``    ``// Allocating a new node in the memory.``    ``struct` `node* n = (``struct` `node*)``        ``malloc``(``sizeof``(``struct` `node));``    ``n->data = ch;``    ``n->left = NULL;``    ``n->right = NULL;``    ``return` `n;``}`` ` `// Function to print Triple Order traversal``void` `tripleOrderTraversal(``struct` `node* root)``{``    ``if` `(root) {`` ` `        ``// Print the current node``        ``cout << root->data << ``" "``;`` ` `        ``// Traverse left subtree``        ``tripleOrderTraversal(root->left);`` ` `        ``// Print the current node``        ``cout << root->data << ``" "``;`` ` `        ``// Traverse right subtree``        ``tripleOrderTraversal(root->right);`` ` `        ``// Print the current node``        ``cout << root->data << ``" "``;``    ``}``}`` ` `// Driver Code``int` `main()``{``    ``struct` `node* root = newNode(``'A'``);``    ``root->left = newNode(``'B'``);``    ``root->right = newNode(``'C'``);``    ``root->left->left = newNode(``'F'``);``    ``root->left->right = newNode(``'D'``);``    ``root->right->right = newNode(``'E'``);`` ` `    ``tripleOrderTraversal(root);``}`

## Java

 `// Java program to implement triple``// order traversal of a binary tree``import` `java.util.*;`` ` `class` `GFG{`` ` `// Structure of a Node``static` `class` `node``{``    ``char` `data;``    ``node left, right;``};`` ` `// Function to create new node``static` `node newNode(``char` `ch)``{``     ` `    ``// Allocating a new node in the memory.``    ``node n = ``new` `node();``    ``n.data = ch;``    ``n.left = ``null``;``    ``n.right = ``null``;``    ``return` `n;``}`` ` `// Function to print Triple Order traversal``static` `void` `tripleOrderTraversal(node root)``{``    ``if` `(root != ``null``)``    ``{``         ` `        ``// Print the current node``        ``System.out.print(root.data + ``" "``);`` ` `        ``// Traverse left subtree``        ``tripleOrderTraversal(root.left);`` ` `        ``// Print the current node``        ``System.out.print(root.data + ``" "``);`` ` `        ``// Traverse right subtree``        ``tripleOrderTraversal(root.right);`` ` `        ``// Print the current node``        ``System.out.print(root.data + ``" "``);``    ``}``}`` ` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``node root = newNode(``'A'``);``    ``root.left = newNode(``'B'``);``    ``root.right = newNode(``'C'``);``    ``root.left.left = newNode(``'F'``);``    ``root.left.right = newNode(``'D'``);``    ``root.right.right = newNode(``'E'``);`` ` `    ``tripleOrderTraversal(root);``}``}`` ` `// This code is contributed by amal kumar choubey `

## Python3

 `# Python3 program to implement triple``# order traversal of a binary tree`` ` `# Structure of node``class` `Node:``     ` `    ``# Initialise the node``    ``def` `__init__(``self``, ch):``         ` `        ``self``.data ``=` `ch``        ``self``.left ``=` `None``        ``self``.right ``=` `None``         ` `# Function to print the Triple Order Traversal``def` `tripleOrderTraversal(root):``     ` `    ``if` `root:``         ` `        ``# Print the current node``        ``print``(root.data, end ``=` `' '``)``         ` `        ``# Print the left subtree``        ``tripleOrderTraversal(root.left)``         ` `        ``# Print the current node``        ``print``(root.data, end ``=` `' '``)``         ` `        ``# Print the right subtree``        ``tripleOrderTraversal(root.right)``         ` `        ``# Print the current node``        ``print``(root.data, end ``=` `' '``)``         ` `# Driver code``root ``=` `Node(``'A'``)``root.left ``=` `Node(``'B'``)``root.right ``=` `Node(``'C'``)``root.left.left ``=` `Node(``'F'``)``root.left.right ``=` `Node(``'D'``)``root.right.right ``=` `Node(``'E'``)`` ` `tripleOrderTraversal(root)``     ` `# This code is contributed by Stuti Pathak`

## C#

 `// C# program to implement triple``// order traversal of a binary tree``using` `System;`` ` `class` `GFG{`` ` `// Structure of a Node``public` `class` `node``{``    ``public` `char` `data;``    ``public` `node left, right;``};`` ` `// Function to create new node``static` `node newNode(``char` `ch)``{``     ` `    ``// Allocating a new node in the memory.``    ``node n = ``new` `node();``    ``n.data = ch;``    ``n.left = ``null``;``    ``n.right = ``null``;``    ``return` `n;``}`` ` `// Function to print Triple Order traversal``static` `void` `tripleOrderTraversal(node root)``{``    ``if` `(root != ``null``)``    ``{``         ` `        ``// Print the current node``        ``Console.Write(root.data + ``" "``);`` ` `        ``// Traverse left subtree``        ``tripleOrderTraversal(root.left);`` ` `        ``// Print the current node``        ``Console.Write(root.data + ``" "``);`` ` `        ``// Traverse right subtree``        ``tripleOrderTraversal(root.right);`` ` `        ``// Print the current node``        ``Console.Write(root.data + ``" "``);``    ``}``}`` ` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``node root = newNode(``'A'``);``    ``root.left = newNode(``'B'``);``    ``root.right = newNode(``'C'``);``    ``root.left.left = newNode(``'F'``);``    ``root.left.right = newNode(``'D'``);``    ``root.right.right = newNode(``'E'``);`` ` `    ``tripleOrderTraversal(root);``}``}`` ` `// This code is contributed by amal kumar choubey `
Output:
```A B F F F B D D D B A C C E E E C A
```

Time Complexity: O(N)
Auxiliary Space: O(1)
Applications: Euler tour of a tree is a modified version of triple order traversal.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up