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 

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

#data structures #recursion #tree #tree-traversal #data analysis

Triple Order Traversal of a Binary Tree
1.35 GEEK