Tree Traversal in JAVA (InOder/preOrder/postOrder)

20:36

Tree Traversal in JAVA (InOder/preOrder/postOrder):-

Tree traversal can be done through three ways.

1)Inorder:-

Go recursively to the left node.
Read a node
Go recursively to the right node.

2)Pre Order.

Read a node
Go recursively to the left node.
Go recursively to the right node.


2)Post Order.


Go recursively to the left node.
Go recursively to the right node.
Read a node



public void inorder(Node root)
 {
  
  if(root==null)
  {
  return;
  }
  
  inorder(root.left);
  System.out.print(root.data+",");
  inorder(root.right);
  
  }
 
 public void preOrder(Node root)
 {
  
  if(root==null)
  {
  return;
  }
  
  System.out.print(root.data+",");
  preOrder(root.left);
  preOrder(root.right);
  
  }
 public void postOrder(Node root)
 {
  
  if(root==null)
  {
  return;
  }
  
  postOrder(root.left);
  
  postOrder(root.right);
  System.out.print(root.data+",");
  }

You Might Also Like

0 comments