在使用迭代遍历二叉树时,使用了栈 以下列出栈的常用方法
-
push 向栈顶添加一个元素
-
pop 从栈顶取出一个元素
-
peek 复制栈顶元素,但不进行删除
-
size 判定栈中所含元素的个数
-
isEmpty 判断栈是否为空
先序遍历二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private List<Integer> list=new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
//递归
/*
if(root==null){
return list;
}
list.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
return list;*/
//迭代
Stack<TreeNode> Stack =new Stack<>();
while(root!=null||!Stack.isEmpty()){
while(root!=null){
list.add(root.val);
Stack.push(root);
root=root.left;
}
root=Stack.pop().right;
}
return list;
}
}
中序遍历二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private List<Integer> res=new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
//递归
/*
if(root==null){
return res;
}
inorderTraversal(root.left);
res.add(root.val);
inorderTraversal(root.right);
return res;
*/
//迭代
Stack<TreeNode> stack=new Stack<>();
while(root!=null||!stack.isEmpty()){
if(root!=null){
stack.push(root);
root=root.left;
}else{
root=stack.pop();
res.add(root.val);
root=root.right;
}
}
return res;
}
}
后序遍历二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private List<Integer> list=new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
//递归
/*
if(root==null){
return list;
}
postorderTraversal(root.left);
postorderTraversal(root.right);
list.add(root.val);
return list; */
//迭代 1.取巧 按根 右 左 把数据放入list中 反转即可
/*Stack<TreeNode> stack=new Stack<>();
while(root!=null||!stack.isEmpty()){
while(root!=null){
list.add(root.val);
stack.push(root);
root=root.right;
}
root=stack.pop().left;
}
Collections.reverse(list);
return list;*/
//迭代 利用pre记录上一个访问过的结点,与当前结点比较,如果是当前结点的子节点,说明其左右结点均已访问,将当前结点出栈,更新pre记录的对象。
if(root == null)
return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode pre = null;
stack.push(root);
while(!stack.isEmpty()){
TreeNode curr = stack.peek();
if((curr.left == null && curr.right == null) ||
(pre != null && (pre == curr.left || pre == curr.right))){
//如果当前结点左右子节点为空或上一个访问的结点为当前结点的子节点时,当前结点出栈
list.add(curr.val);
pre = curr;
stack.pop();
}else{
if(curr.right != null) stack.push(curr.right); //先将右结点压栈
if(curr.left != null) stack.push(curr.left); //再将左结点入栈
}
}
return list;
}
}
总结
递归得到代码实现简单 不同的遍历顺序 只要改变代码的顺序即可
迭代 用到数据结构栈 只需要按一定逻辑 运用栈 先进后出的逻辑即可。