迭代法遍历二叉树

本文详细介绍了如何使用非递归方法遍历二叉树,包括先序、中序和后序遍历的具体实现,利用栈来替代递归调用,提供了一种节省内存、易于理解的算法解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不用递归的方法取遍历二叉树
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/

先序遍历

思路如下:

  1. 先创建一个栈
  2. 把根节点入栈
  3. 循环取栈顶的元素并且访问它
  4. 把当前元素的右子树和左子树分别入栈
/**
 * 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> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root == null){
            return result;
        }

        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode cur = stack.pop();
            result.add(cur.val);
            if(cur.right != null){
                stack.push(cur.right);
            }
            if(cur.left != null){
                stack.push(cur.left);
            }
        }
        return result;
    }
}

这道题的核心是将根节点先入栈,然后取栈顶元素访问,插入右子树,插入左子树,就可以实现啦

中序遍历

核心思路

  1. 需要创建一个cur指向来在树上移动
  2. 如果cur不为空,一路push,一路向左
  3. cur为空时,取出栈顶元素,并且 cur = top.right
/**
 * 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> result = new ArrayList<>();
        if(root == null){
            return result;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (true){
            while (cur != null){
                 stack.push(cur);
                 cur = cur.left;
            }
            //走到这一步,cur 为空,取栈顶元素
            if(stack.isEmpty()){
                break;
            }
            TreeNode top = stack.pop();
            result.add(top.val);
            cur = top.right;
        }
        return result;
    }
}

后序遍历

https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
这个就有点复杂了

/**
 * 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) {
        Stack<TreeNode> stack = new Stack<>();
        List<Integer> result = new ArrayList<>();
        if(root == null){
            return result;
        }
        TreeNode cur = root;
        TreeNode prev = null;
        while (true){
            while (cur != null){
                //cur不为空就一路入栈一路向左
                stack.push(cur);
                cur = cur.left;
            }
            //此时cur 为空
            if(stack.isEmpty()){
                break;//遍历结束
            }
            TreeNode top = stack.peek();
            if(top.right == null || top.right == prev){
                //这种情况说明可以访问top节点
                TreeNode rTop = stack.pop();
                result.add(rTop.val);
                prev = top;

            }else {
                cur = top.right;
            }
        }
        return result;
    }
}

在取栈顶元素不能直接访问了,需要判断,如果这个元素右子树为空或者右子树访问过了,就可以访问

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值