##第2关
二叉树的实现之中序遍历
任务描述
在上一关,我们实现了二叉树的前序遍历,本关我们将实现二叉树的中序遍历。
本关任务:实现以二叉链表的二叉树的中序遍历算法。
package step2;
public class BinaryTree {
private TreeNode root;//根节点
public BinaryTree() {
root = null;
}
public void inOrder(TreeNode root) {
/********** Begin /
if(root!=null) {
inOrder(root.leftChild);
System.out.println(root.item);
inOrder(root.rightChild);
}
/* End *********/
}
public TreeNode createTree(int arr[]) {
TreeNode tmp[] = new TreeNode[arr.length + 1];
for (int k = 1; k <= arr.length; k++) {
TreeNode node = new TreeNode(arr[k - 1]);
tmp[k] = node;
if (k == 1) {
root = node;
} else { int j = k / 2; if (k % 2 == 0) { tmp[j].leftChild = node; } else { tmp[j].rightChild = node; } } } return root; } public static class TreeNode { private TreeNode leftChild; private TreeNode rightChild; private int item; public TreeNode(int item) { this(null, null, item); } public TreeNode(TreeNode leftChild, TreeNode rightChild, int item) { this.leftChild = leftChild; this.rightChild = rightChild; this.item = item; } }}