# Java中用递归和迭代实现二叉树的中序（ InOrder )遍历

InOrder遍历算法首先访问左节点，然后是root节点，最后是右节点。这与首先​​访问根的预序遍历不同。InOrder遍历的一个重要特性是，如果给定的二叉树是二叉搜索树，它将按排序顺序打印节点。

## Java中InOrder遍历二叉树 - 递归

1. 访问左节点
2. 访问root
3. 访问右节点

• 在inOrder（TreeNode节点）中编写方法

• 检查node == null，如果是，则返回，这是我们的基本情况。

• 调用inOrder（node.left）以递归方式访问左子树

• 打印节点的值

• 调用inOrder（node.right）以递归方式遍历右子树。

``````private void inOrder(TreeNode node) {
if (node == null) {
return;
}

inOrder(node.left);
System.out.printf("%s ", node.data);
inOrder(node.right);
}
``````

## Java中InOrder遍历二叉树 - 迭代

``````
4
/ \
2   5
/ \   \
1   3   6

public void inOrderWithoutRecursion() {
Stack nodes = new Stack<>();
TreeNode current = root;

while (!nodes.isEmpty() || current != null) {

if (current != null) {
nodes.push(current);
current = current.left;
} else {
TreeNode node = nodes.pop();
System.out.printf("%s ", node.data);
current = node.right;
}

}
}

Output
1 2 3 4 5 6
``````

## Java中使用InOrder算法遍历二叉树

``````import java.util.Stack;

/*
* Java Program to traverse a binary tree
* using inorder traversal without recursion.
* In InOrder traversal first left node is visited, followed by root
* and right node.
*
* input:
*     4
*    / \
*   2   5
*  / \   \
* 1   3   6
*
* output: 1 2 3 4 5 6
*/

public class InOrderTraversal {

public static void main(String args) throws Exception {

// construct the binary tree given in question
BinaryTree bt = BinaryTree.create();

// traversing binary tree using InOrder traversal using recursion
System.out
.println("printing nodes of a binary tree on InOrder using recursion");

bt.inOrder();

System.out.println(); // insert new line

// traversing binary tree on InOrder traversal without recursion
System.out
.println("printing nodes of binary tree on InOrder using iteration");
bt.inOrderWithoutRecursion();
}

}

class BinaryTree {
static class TreeNode {
String data;
TreeNode left, right;

TreeNode(String value) {
this.data = value;
left = right = null;
}

boolean isLeaf() {
return left == null ? right == null : false;
}

}

// root of binary tree
TreeNode root;

/**
* traverse the binary tree on InOrder traversal algorithm
*/
public void inOrder() {
inOrder(root);
}

private void inOrder(TreeNode node) {
if (node == null) {
return;
}

inOrder(node.left);
System.out.printf("%s ", node.data);
inOrder(node.right);
}

public void inOrderWithoutRecursion() {
Stack nodes = new Stack<>();
TreeNode current = root;

while (!nodes.isEmpty() || current != null) {

if (current != null) {
nodes.push(current);
current = current.left;
} else {
TreeNode node = nodes.pop();
System.out.printf("%s ", node.data);
current = node.right;
}

}
}

/**
* Java method to create binary tree with test data
*
* @return a sample binary tree for testing
*/
public static BinaryTree create() {
BinaryTree tree = new BinaryTree();
TreeNode root = new TreeNode("4");
tree.root = root;
tree.root.left = new TreeNode("2");
tree.root.left.left = new TreeNode("1");

tree.root.left.right = new TreeNode("3");
tree.root.right = new TreeNode("5");
tree.root.right.right = new TreeNode("6");

return tree;
}

}

Output
printing nodes of a binary tree on InOrder using recursion
1 2 3 4 5 6
printing nodes of a binary tree on InOrder using iteration
1 2 3 4 5 6
``````