思路:
二叉树的中序非递归遍历和前序非递归遍历类似。
代码如下:
import java.util.*;
public class Solution {
public ArrayList<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> array=new ArrayList();
Stack<TreeNode> stack=new Stack();
if(root == null)
return array;
TreeNode node=root;
while(node!=null||!stack.empty())
{
while(node!=null)
{
stack.push(node);
node=node.left;
}
if(node == null&&!stack.empty())
{
TreeNode temp=stack.pop();
array.add(temp.val);
node=temp.right;
}
}
return array;
}
}
本文介绍了一种使用非递归方式实现二叉树中序遍历的方法,并提供了详细的Java代码实现。通过栈结构来辅助遍历过程,有效地实现了二叉树节点的访问。
2793

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



