2019.3.36
//exercise 25.3
public void inorderUsingStack(){
inorderUsingStack(root);
}
private void inorderUsingStack(TreeNode<T> root){
if(root == null)
return;
Stack<TreeNode<T>> stack = new Stack<>();
TreeNode<T> current = root;
while(current != null || !stack.isEmpty()){
if(current != null){
stack.push(current);
current = current.left;
}
else{
current = stack.pop();
System.out.print(current.element+" ");
current = current.right;
}
}
}

本文介绍了一种使用栈实现二叉树中序遍历的方法。通过不断将当前节点压入栈并转向左子树,直到左子树为空。然后从栈中弹出节点并打印,再转向右子树继续遍历。
3万+

被折叠的 条评论
为什么被折叠?



