1
/ \
2 3
/ \
4 5
package tree;
import java.util.Stack;
public class InorderTreeTraversal {
/**
* 带栈的二叉树的非递归中序遍历
* @param args
*/
public static void inorderTree(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
while(root!=null||(!stack.isEmpty())){
while(root!=null){
stack.push(root);
root = root.left;
}
TreeNode node =stack.pop();
System.out.print(node.value+" ");
root = node.right;
}
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
inorderTree(root);
}
}