# In-order Traversal of a Binary Tree

Given a Binary Tree, print the nodes of a binary tree in in-order fashion.

## Algorithm/Insights

In the in-order traversal for a given node 'n',
1. We first traverse left-subtree of 'n' by calling in_order_print(n.left)
2. Then we visit node 'n' itself.
3. And finally we traverse right-subtree of 'n' by calling in_order_print(n.right)

Please check out the code below(in_order_print()) with visualization for illustration.

## Code Snippet

```
package com.ideserve.nilesh.questions;

public class Tree {

private TreeNode root;

public TreeNode getRoot() {
return root;
}

class TreeNode {

private int data;
private TreeNode left;
private TreeNode right;

public TreeNode(int data, TreeNode left, TreeNode right) {
super();
this.data = data;
this.left = left;
this.right = right;
}

public int getData() {
return data;
}

public void setData(int data) {
this.data = data;
}

public TreeNode getLeft() {
return left;
}

public void setLeft(TreeNode left) {
this.left = left;
}

public TreeNode getRight() {
return right;
}

public void setRight(TreeNode right) {
this.right = right;
}

public TreeNode() {
super();
}

public TreeNode(int data) {
super();
this.data = data;
}

@Override
public String toString() {
return data+"";
}
}

public static void main(String[] args) {
Tree tree = new Tree();
tree.createSampleTree();
tree.printInorder();
}

/*
* Create a sample tree
* 				1
* 		2				3
* 4		5		6		7
*
*/
public void createSampleTree() {
root = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, new TreeNode(6), new TreeNode(7)));
}

/*
* Print inorder traversal
*/
public void printInorder() {
printInorder(root);
}

private void printInorder(TreeNode root) {
if(root == null) {
return;
}
printInorder(root.getLeft());
System.out.print(root.getData() + " ");
printInorder(root.getRight());
}
}
```

## Order of the Algorithm

Time Complexity is O((n))
Space Complexity is O((1))