您可以将其更改为Iteration而不是递归吗?
您可以使用显式堆栈。伪代码:
private static void iterateall(BinaryTree foo) { Stack<BinaryTree> nodes = new Stack<BinaryTree>(); nodes.push(foo); while (!nodes.isEmpty()) { BinaryTree node = nodes.pop(); if (node == null) continue; System.out.println(node.node); nodes.push(node.right); nodes.push(node.left); }}但这并不真正优于递归代码(除了代码中缺少的基本条件)。



