二叉树的前中后序遍历(递归加迭代)

二叉树的前中后序遍历基于DFS深度优先遍历,递归写法很简单,迭代写法比较难。
递归法:

  1. 前序遍历
public void preDfs(TreeNode root) {
	if(root != null) {
		visit(root);
		preDfs(root.left);
		preDfs(root.right);
	} 
}
  1. 中序遍历
public void inDfs(TreeNode root) {
	if(root != null) {
		inDfs(root.left);
		visit(root);
		inDfs(root.right);
	} 
}
  1. 后序遍历
public void postDfs(TreeNode root) {
	if(root != null) {
		postDfs(root.left);
		postDfs(root.right);
		visit(root);
	} 
}

迭代法:
基于dfs就要用到栈数据结构。

  1. 前序遍历
    遍历顺序为根左右,刚好可以边弹出节点,边压入右节点和左节点,因为栈的特性所以要注意两者的顺序。
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root == null) return res;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()) {
            TreeNode temp = stack.pop();
            res.add(temp.val);
            if(temp.right != null) stack.push(temp.right);
            if(temp.left != null) stack.push(temp.left);
        }
        return res;
    }
}

// 方法二 和下面中序遍历的思路差不多。只不过是压入栈之前就遍历节点,而中序遍历弹出之后再遍历。
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null) return result;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                result.add(cur.val);
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            cur =  cur.right;
        }
        return result;
    }
}
  1. 中序遍历
    遍历顺序为左根右,因为根在中间,所以不能先弹出根了。跟前序遍历不大一样,压入根节点后一直压入根节点的左子节点,之后弹出并访问最左边的节点,然后访问右节点进入下一个大循环,这里用curNode记录当前处理的节点,到这里一个循环。
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root == null) return res;
        TreeNode curNode = root;
        while(curNode != null || !stack.isEmpty()) {
            while(curNode != null) {
                stack.push(curNode);
                curNode = curNode.left;
            }
            curNode = stack.pop();
            res.add(curNode.val);
            curNode = curNode.right;
        }
        return res;
    }
}
  1. 后序遍历
    遍历顺序为左右根,这里用了比较讨巧的方式,LinkedList允许在列表最前面插入值,那么就可以从根节点弹出并访问值,只不过插入的顺序总是在最前面。插入根的值后应该插入右子树的值了,所以右子树应该比左子树后入栈。
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        LinkedList<Integer> res = new LinkedList<>();
        if(root == null) return res;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()) {
            TreeNode temp = stack.pop();
            res.addFirst(temp.val);
            if(temp.left != null) stack.push(temp.left);
            if(temp.right != null) stack.push(temp.right);
        }
        return res;
    }
}

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null) return result;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode pre = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.peek();
            if (cur.right == null || pre == cur.right) {
                result.add(cur.val);
                stack.pop();
                pre = cur;
                cur = null;
            } else {
                cur = cur.right;
                pre = null;
            }
        }
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值