二叉树的前序遍历、中序遍历、后序遍历,其中包括递归的方法和非递归(利用栈结构)的方法。
public
List
<
Integer
> inorderTraversal(TreeNode root) {List < Integer > list = new ArrayList< Integer >();
Stack <TreeNode> stack = new Stack <TreeNode>();
TreeNode cur = root;
while (cur!= null || ! stack .empty()){
while (cur!= null ){
stack .add(cur);
cur = cur.left;
}
cur = stack .pop();
list .add(cur.val);
cur = cur.right;
}
return list ;
}