二叉树的Morris遍历(常数空间复杂度)、二叉树的非递归遍历(使用栈)

本文详细介绍了二叉树的前序、中序和后序 Morris 遍历算法,通过非递归方式高效地遍历树结构,并提供了相应的 Java 实现代码。 Morris 算法利用了指向父节点的额外链接,减少了辅助空间的使用,对于理解二叉树的遍历有很好的启示作用。
摘要由CSDN通过智能技术生成

1、二叉树的前序遍历(Morris)

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        TreeNode p1 = root, p2 = null;
        while(p1 != null) {
            p2 = p1.left;
            if(p2 != null) {
                while(p2.right != null && p2.right != p1) {
                    p2 = p2.right;
                }
                if(p2.right == null) {
                    ans.add(p1.val);
                    p2.right = p1;
                    p1 = p1.left;
                    continue;
                } else {
                    p2.right = null;
                }
            } else {
                ans.add(p1.val);
            }
            p1 = p1.right;
        }
        return ans;
    }
}

2、二叉树的中序遍历(Morris)

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        TreeNode p1 = root, p2 = null;
        while(p1 != null) {
            p2 = p1.left;
            if(p1.left != null) {
                while(p2.right != null && p2.right != p1) {
                    p2 = p2.right;
                }
                if(p2.right != null) {
                    p2.right = null;
                    ans.add(p1.val);
                    p1 = p1.right;
                } else {
                    p2.right =p1;
                    p1 = p1.left;
                }
            } else {
                ans.add(p1.val);
                p1 = p1.right;
            }
        }
        return ans;
    }
}

3、二叉树的后序遍历(Morris)

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if(root == null) return ans;
        TreeNode p1 = root, p2 = null;
        while(p1 != null) {
            p2 = p1.left;
            if(p2 != null) {
                while(p2.right != null && p2.right != p1) {
                    p2 = p2.right;
                }
                if(p2.right == null) {
                    p2.right = p1;
                    p1 = p1.left;
                    continue;
                } else {
                    p2.right = null;
                    addPath(ans, p1.left);
                }
            }
            p1 = p1.right;
        }
        addPath(ans, root);
        return ans;
    }
    private void addPath(List<Integer> ans, TreeNode node) {
        int count = 0;
        while(node != null) {
            ans.add(node.val);
            node = node.right;
            count++;
        }
        int left = ans.size() - count, right = ans.size() - 1;
        while(left < right) {
            int tmp = ans.get(left);
            ans.set(left, ans.get(right));
            ans.set(right, tmp);
            left++;
            right--;
        }
    }
}

4、二叉树的后序非递归

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if(root == null) return ans;
        Deque<TreeNode> stack = new LinkedList<>();
        TreeNode pre = null;
        while(root != null || !stack.isEmpty()) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if(root.right == null || root.right == pre) {
                ans.add(root.val);
                pre = root;
                root = null;
            } else {
                stack.push(root);
                root = root.right;
            }
        }
        return ans;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值