Saral Code logo
Saral Code
    HomeBlogTutorialsMCQs

No Related Topics Available


In Linked Representation of Binary Tree, there are 3 ways to traverse: -

  • PreOrder Traversal
  • InOrder Traversal
  • Postorder Traversal

Traversal

PreOrder Traversal

In PreOrder Traversal, the root node is visited first, then the left subtree, and finally the right subtree.

Order : Root → Left Subtree → Right Subtree

PreOrder Traversal in Binary Tree in Data Structure and Algorithm - dsa

From the above image data: -

  • Root Node: 1
  • Left SubTree:
    • Root Node: 2
    • Left SubTree: 4
      • No node
    • Right SubTree: 5
      • No Node
  • Right SubTree:
    • Root Node: 3
    • Left SubTree: 6
      • No node
    • Right SubTree: 7
      • No Node

Output: 1 2 4 5 3 6 7

Look at the structure above, In PreOrder Traversal first, we have to visit the root node, then the Left Subtree, and keep visiting Left Subtree and repeat the process until it has No Node, then go back from there and visit Right Subtree and again follow the same process.

InOrder Traversal

In InOrder Traversal, the left subtree is visited first, then the root node, and finally the right subtree.

Order : Left Subtree → Root → Right Subtree

InOrder Traversal in Binary Tree in Data Structure and Algorithm - DSA

From the above image data: -

  • Left SubTree:
    • Left SubTree: 4
      • No node
    • Root Node: 2
    • Right SubTree: 5
      • No Node
  • Root Node: 1
  • Right SubTree:
    • Left SubTree: 6
      • No node
    • Root Node: 3
    • Right SubTree: 7
      • No Node

Output: 4 2 5 1 6 3 7

In InOrder Traversal first, we have to keep visiting Left Subtree and repeat the process until it has No Node and then go back to the root node after that, visit the Right Subtree and again follow the same process.

Postorder Traversal

In PostOrder Traversal, the left subtree is visited first, then the right subtree, and finally the root node.

Order : Left Subtree → Right Subtree → Root

PostOrder Traversal in Binary Tree in Data Structure and Algorithm - DSA

From the above image data: -

  • Left SubTree:
    • Left SubTree: 4
      • No node
    • Right SubTree: 5
      • No Node
    • Root Node: 2
  • Right SubTree:
    • Left SubTree: 6
      • No node
    • Right SubTree: 7
      • No Node
    • Root Node: 3
  • Root Node: 1

Output: 4 5 2 6 7 3 1

In PostOrder Traversal first, we have to keep visiting Left Subtree and repeat the process until it has No Node. Then go to Right Subtree and again repeat the process and when there is No Node, visit the root node.

Traversal Tricks Trick

In these tricks, We draw a line from the root node that travels near every node and draw an arrow in every node that cuts the line according to the order.

You will understand from the below images.

PreOrder

In PreOrder, We have to Draw an arrow in Left Direction from the Node that cuts the line.

PreOrder Traversal Trick in Binary Tree in DSA

By following the line from the root node: -

Result: 1 2 4 5 3 6 7

InOrder

In InOrder, We have to Draw an arrow in Bottom Direction from the Node that cuts the line.

InOrder Traversal Trick in Binary Tree - DSA

By following the line from the root node: -

Result: 4 2 5 1 6 3 7

Postorder

In PostOrder, We have to Draw an arrow in the Right Direction from the Node that cuts the line.

Post Order Traversal Trick in Binary Tree in DSA

By following the line from the root node: -

Result: 4 5 2 6 7 3 1