Triple Order Traversal of a Binary Tree
Last Updated :
21 Oct, 2024
Given a Binary Tree, the task is to find its 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:

Output: 1 7 4 4 4 7 5 5 5 7 1 3 6 6 6 3 3 1
Explanation: The root (1) is visited, followed by its left subtree (7), where nodes 4 and 5 are each visited thrice before returning to the root, visiting the right subtree (3), where node 6 is visited thrice.
Input:

Output: 1 7 4 4 4 7 5 5 5 7 1 3 3 6 6 6 3 1
Explanation: The root (1) is visited, followed by its left subtree (7), where nodes 4 and 5 are each visited thrice before returning to the root, visiting the right subtree (3), where node 6 is visited thrice.
Approach:
The idea is to traverse a binary tree while storing each node's value multiple times in the traversal. We first store the node's value, then recursively visit the left subtree, store the value again, and finally visit the right subtree before storing the value once more. And finally print the stored values.
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, store the current node.
- Recursively traverse the right subtree.
- Again, store 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++ implementation for printing triple order
// traversal of a Tree
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int x) {
data = x;
left = nullptr;
right = nullptr;
}
};
// Function to perform triple order traversal
vector<int> tripleOrderTraversal(Node* root) {
vector<int> result;
if (!root) return result;
// Store node value before traversing
// left subtree
result.push_back(root->data);
// Recursively traverse the left subtree
vector<int> leftSubtree
= tripleOrderTraversal(root->left);
result.insert(result.end(),
leftSubtree.begin(), leftSubtree.end());
// Print node value again after
// traversing left subtree
result.push_back(root->data);
// Recursively traverse the right subtree
vector<int> rightSubtree
= tripleOrderTraversal(root->right);
result.insert(result.end(),
rightSubtree.begin(), rightSubtree.end());
// Print node value once more after
// traversing the right subtree
result.push_back(root->data);
return result;
}
int main() {
// Representation of the binary tree
// 1
// / \
// 7 3
// / \ /
// 4 5 6
Node* root = new Node(1);
root->left = new Node(7);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->left = new Node(6);
vector<int> result
= tripleOrderTraversal(root);
for (int num : result) {
cout << num << " ";
}
return 0;
}
Java
// Java implementation for printing triple order
// traversal of a Tree
import java.util.*;
class Node {
int data;
Node left;
Node right;
Node(int x) {
data = x;
left = null;
right = null;
}
}
// Function to perform triple order traversal
class GfG {
static List<Integer>
tripleOrderTraversal(Node root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
// Store node value before traversing
// left subtree
result.add(root.data);
// Recursively traverse the left subtree
List<Integer> leftSubtree =
tripleOrderTraversal(root.left);
result.addAll(leftSubtree);
// Print node value again after
// traversing left subtree
result.add(root.data);
// Recursively traverse the right subtree
List<Integer> rightSubtree =
tripleOrderTraversal(root.right);
result.addAll(rightSubtree);
// Print node value once more after
// traversing the right subtree
result.add(root.data);
return result;
}
public static void main(String[] args) {
// Representation of the binary tree
// 1
// / \
// 7 3
// / \ /
// 4 5 6
Node root = new Node(1);
root.left = new Node(7);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
List<Integer> result =
tripleOrderTraversal(root);
for (int num : result) {
System.out.print(num + " ");
}
}
}
Python
# Python implementation for printing triple order
# traversal of a Tree
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# Function to perform triple order traversal
def triple_order_traversal(root):
result = []
if not root:
return result
# Store node value before traversing
# left subtree
result.append(root.data)
# Recursively traverse the left subtree
left_subtree = triple_order_traversal(root.left)
result.extend(left_subtree)
# Store node value again after
# traversing left subtree
result.append(root.data)
# Recursively traverse the right subtree
right_subtree = triple_order_traversal(root.right)
result.extend(right_subtree)
# Store node value once more after
# traversing the right subtree
result.append(root.data)
return result
if __name__ == "__main__":
# Representation of the binary tree
# 1
# / \
# 7 3
# / \ /
# 4 5 6
root = Node(1)
root.left = Node(7)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
result = triple_order_traversal(root)
print(" ".join(map(str, result)))
C#
// C# implementation for printing triple order
// traversal of a Tree
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left;
public Node right;
public Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Function to perform triple order traversal
static List<int>
TripleOrderTraversal(Node root) {
List<int> result = new List<int>();
if (root == null) return result;
// Store node value before traversing
// left subtree
result.Add(root.data);
// Recursively traverse the left subtree
List<int> leftSubtree =
TripleOrderTraversal(root.left);
result.AddRange(leftSubtree);
// Store node value again after
// traversing left subtree
result.Add(root.data);
// Recursively traverse the right subtree
List<int> rightSubtree =
TripleOrderTraversal(root.right);
result.AddRange(rightSubtree);
// Store node value once more after
// traversing the right subtree
result.Add(root.data);
return result;
}
static void Main(string[] args) {
// Representation of the binary tree
// 1
// / \
// 7 3
// / \ /
// 4 5 6
Node root = new Node(1);
root.left = new Node(7);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
List<int> result =
TripleOrderTraversal(root);
foreach (int num in result) {
Console.Write(num + " ");
}
}
}
JavaScript
// JavaScript implementation for printing triple order
// traversal of a Tree
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// Function to perform triple order traversal
function tripleOrderTraversal(root) {
let result = [];
if (root === null) return result;
// Store node value before traversing
// left subtree
result.push(root.data);
// Recursively traverse the left subtree
let leftSubtree = tripleOrderTraversal(root.left);
result = result.concat(leftSubtree);
// Store node value again after
// traversing left subtree
result.push(root.data);
// Recursively traverse the right subtree
let rightSubtree = tripleOrderTraversal(root.right);
result = result.concat(rightSubtree);
// Store node value once more after
// traversing the right subtree
result.push(root.data);
return result;
}
// Representation of the binary tree
// 1
// / \
// 7 3
// / \ /
// 4 5 6
let root = new Node(1);
root.left = new Node(7);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
let result = tripleOrderTraversal(root);
console.log(result.join(" "));
Output1 7 4 4 4 7 5 5 5 7 1 3 6 6 6 3 3 1
Time Complexity: O(n), where n is the number of nodes in the tree.
Auxiliary Space: O(h), where h is the height of the tree.
Applications: Euler tour of a tree is a modified version of triple order traversal.
Similar Reads
Preorder Traversal of Binary Tree Preorder traversal is a tree traversal method that follows the Root-Left-Right order:The root node of the subtree is visited first.Next, the left subtree is recursively traversed.Finally, the right subtree is recursively traversed.How does Preorder Traversal work?Key Properties: Used in expression t
5 min read
Double Order Traversal of a Binary Tree Given a Binary Tree, the task is to find its Double Order Traversal. Double Order Traversal is a tree traversal technique in which every node is traversed twice in the following order: Visit the Node.Traverse the Left Subtree.Visit the Node.Traverse the Right Subtree.Examples:Input: Output: 1 7 4 4
6 min read
Vertical Traversal of a Binary Tree Given a Binary Tree, the task is to find its vertical traversal starting from the leftmost level to the rightmost level. If multiple nodes pass through a vertical line, they should be printed as they appear in the level order traversal of the tree.Examples: Input:Output: [[4], [2], [1, 5, 6], [3, 8]
10 min read
Middle To Up-Down Order traversal of a Binary Tree Given a binary tree, the task is to traverse this binary tree from the middle to the up-down order. In Middle to up-down order traversal, the following steps are performed: First, print the middle level of the tree.Then, print the elements at one level above the middle level of the tree.Then, print
15+ min read
Inorder Traversal of Binary Tree Inorder traversal is a depth-first traversal method that follows this sequence:Left subtree is visited first.Root node is processed next.Right subtree is visited last.How does Inorder Traversal work?Key Properties:If applied to a Binary Search Tree (BST), it returns elements in sorted order.Ensures
5 min read