2016-06-21

Tree Preorder Traversal using Java



/*
Node is defined as

class Node {
    int data;
    Node left;
    Node right;
}
*/




  1. void Preorder(Node root) {
  2. System.out.print(root.data + " ");

  3.     if (root.left != null) {
  4.         Preorder(root.left);
  5.     } 
  6.     
  7.     if (root.right != null) {
  8.         Preorder(root.right);
  9.     }
  10. }

No comments:

Post a Comment